c++glfwautotoolsundefined-referencemsys2

Why am I getting: "undefined reference to `glfwInit'"?


Whenever I try to compile my OpenGL sample project via MSYS2, Autotools and then make (which uses statically compiled GLEW and GLFW) I am getting this kind of output:

x86_64-w64-mingw32-g++ -DHAVE_CONFIG_H -I. -I./include  -I./include/GL -I./include/GLFW -DGLEW_STATIC   -g -O2 -MT src/opengl_tut-main.o -MD -MP -MF src/.deps/opengl_tut-main.Tpo -c -o src/opengl_tut-main.o `test -f 'src/main.cpp' || echo './'`src/main.cpp
mv -f src/.deps/opengl_tut-main.Tpo src/.deps/opengl_tut-main.Po
x86_64-w64-mingw32-g++  -g -O2 -L./lib -lglfw3 -lglew32 -lopengl32 -lgdi32  -o opengl_tut.exe src/opengl_tut-main.o
src/opengl_tut-main.o: In function `main':
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:13: undefined reference to `glfwInit'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:21: undefined reference to `glfwWindowHint'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:22: undefined reference to `glfwWindowHint'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:24: undefined reference to `glfwWindowHint'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:25: undefined reference to `glfwWindowHint'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:27: undefined reference to `glfwCreateWindow'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:41: undefined reference to `glfwGetFramebufferSize'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:45: undefined reference to `glfwMakeContextCurrent'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:49: undefined reference to `glewInit'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:58: undefined reference to `glViewport'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:62: undefined reference to `glfwPollEvents'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:64: undefined reference to `glClearColor'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:65: undefined reference to `glClear'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:67: undefined reference to `glfwSwapBuffers'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:60: undefined reference to `glfwWindowShouldClose'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:16: undefined reference to `glfwTerminate'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:52: undefined reference to `glfwDestroyWindow'
C:\Users\Test\Desktop\OpenGL_2/src/main.cpp:53: undefined reference to `glfwTerminate'
C:\Users\DoTestn\Desktop\OpenGL_2/src/main.cpp:35: undefined reference to `glfwTerminate'
src/opengl_tut-main.o:main.cpp:(.rdata$.refptr.glewExperimental[.refptr.glewExperimental]+0x0): undefined reference to `glewExperimental'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:361: opengl_tut.exe] Error 1  

What I do not understand is that I am able to compile my code with manually typed command which looks like this:

x86_64-w64-mingw32-g++ -ggdb -Og -std=c++14 src/main.cpp -o bin/opengl_tut.exe -DGLEW_STATIC -I./include/GL -I./include/GLFW -L./lib -lglfw3 -lglew32 -lopengl32 -lgdi32  

However, I would prefer to achieve the same result with Autotools.


Solution

  • The src/opengl_tut-main.o object probably depends on OpenGL libraries, so it must be listed before the OpenGL libraries in the final command that links everything together.