Linking & Compiling GLFW with MinGW

Hi, I have tried for days to get GLFW to open a window in Visual Studio Code using mingw64. I have read every forum post and nothing has worked so far. I had it easily working with VS 2022 but not with VS Code.

I get an “undefined reference to `glfwInit’” error even though libglfw3.a (64 bit version) is installed and found by the compiler. I think the error has something to do with the arguments for the compiler in my tasks.json file.

I have installed mingw64 with msys64 and can successfully compile and build 64-bit c++ programs in VS Code.

I have only done what I have mentioned here, so I don’t have any magical glfw and OpenGL files floating around anywhere, besides the default opengl32.lib file that is located in the system32 windows folder.

I am using windows 10.

File structure:

Project
   .vscode
      tasks.json
   Dependencies
      include
         glfw3.h
         glfw3native.h
      lib
         libglfw3.a

Here is my code:

#include <iostream>
#include <GLFW/glfw3.h>

int main() {
    glfwInit();
    std::cout << "Hello World!\n";
    std::cin.get();
    return 0;
}

Here are the arguments for my tasks.json file:

"args": [
		"-fdiagnostics-color=always",
		"-g",
		"-I${workspaceFolder}/Dependencies/include",
		"-L${workspaceFolder}/Dependencies/lib",
		"-lglfw3","-lopengl32","-lgdi32", "-luser32", "-lkernel32",
		"${file}",
		"-o",
		"${fileDirname}\\${fileBasenameNoExtension}.exe"
	],

Terminal Output

Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g -IC:\projects\OpenGL_Test/Dependencies/include -LC:\projects\OpenGL_Test/Dependencies/lib -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32 C:\projects\OpenGL_Test\Application.cpp -o C:\projects\OpenGL_Test\Application.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\willi\AppData\Local\Temp\ccVOsapa.o:C:\projects\OpenGL_Test/Application.cpp:5: undefined reference to `glfwInit'

collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

I’m having the same issue. Were you able to solve it?

No, I’m still having the same problem. I have given up and I’m currently continuing my work in visual studio 2022 instead.

With compilers like GCC you need to list files and libraries before the files and libraries they depend on. In the command-line above your source file is listed after the GLFW library and the system libraries. Since there is nothing listed after the source file that defines glfwInit the link fails.

Moving the source file before the libraries should fix this problem.

1 Like

lol. same. i gave up after 3-4days of trying and installed vs

Hey!
Can you please! Provide more contexts.

Getting same errors. lol

Hi,

Please ensure that your source or object files (*.cpp, *.c, *.o) are positioned before the library dependencies (-L, -l). The order of the arguments passed to the compiler has an effect in this situation.

Here is how the final command that gets passed could look like:

g++ -Iinclude src/main.cpp -o build/main.exe -Llib -lglfw3 -lopengl32 -lgdi32
1 Like

in tasks.json
please put the lib “-lglfw3”,“-lopengl32”,“-lgdi32”, “-luser32”, “-lkernel32”,
behind the “${fileDirname}\${fileBasenameNoExtension}.exe”,
or at the end of this bracelet
hope that helps