无法通过MinGW-w64与GLFW 3链接

时间:2019-03-06 12:52:57

标签: c++ glfw mingw-w64

在MinGW-w64中使用GLFW的编译代码,直到链接阶段一切正常。此时,即使我指定了其他库目录和使用过的库,ld也开始抱怨对GLFW函数的未定义引用。这是我的g++调用(当然,我在批处理文件中使用了很多环境变量来缩短命令,这里是未包装的):

x86_64-w64-mingw32-g++.exe -I./deps/glfw-3.2.1/include -I./deps/gl3w/include -I./include -L./deps/glfw-3.2.1/lib-mingw-w64/x86_64 -lglfw3 -lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32 -std=c++14 -fpermissive -mwindows -o build\out\windows\Release\x86_64\GemVerse.exe ./deps/gl3w/src/gl3w.c ./src/main.cpp

以下是相同的命令,已分解:

x86_64-w64-mingw32-g++.exe %INCLUDEDIRS% %LIBDIRS64% %CFLAGS% %LIBS% -o build\out\windows\Release\x86_64\GemVerse.exe %SOURCES%

%INCLUDEDIRS%=-I./deps/glfw-3.2.1/include -I./deps/gl3w/include -I./deps/glm-0.9.9.3/include -I./include%LIBDIRS64%=-L./deps/glfw-3.2.1/lib-mingw-w64/x86_64%LIBS%=-lglfw3 -lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32%CFLAGS%=-std=c++14 -fpermissive -mwindows%SOURCES%=./deps/gl3w/src/gl3w.c ./src/main.cpp的地方

如您所见,已找到并链接了库,但符号仍未解析。这怎么可能?如何正确链接GLFW?


编辑1:

我更改了库顺序,现在为-lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32 -glfw3,因此GLFW位于末尾。但这不能解决问题,ld仍然找不到定义。这是g++日志,因为我似乎没有附加日志,所以我的错:

./deps/gl3w/src/gl3w.c: In function 'int open_libgl()':
./deps/gl3w/src/gl3w.c:47:52: warning: invalid conversion from 'FARPROC' {aka 'long long int (*)()'} to 'void*' [-fpermissive]
  *(void **)(&wgl_get_proc_address) = GetProcAddress(libgl, "wglGetProcAddress");
                                      ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x65): undefined reference to `glfwInit'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x74): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x83): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x92): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x9e): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0xc0): undefined reference to `glfwCreateWindow'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0xd4): undefined reference to `glfwMakeContextCurrent'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x1b): undefined reference to `glfwTerminate'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x3b): undefined reference to `glfwGetKey'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x5c): undefined reference to `glfwSwapBuffers'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x61): undefined reference to `glfwPollEvents'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x66): undefined reference to `glfwTerminate'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x7d): undefined reference to `glfwSetWindowShouldClose'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

由于我项目中的初始化代码被封装到一个类中,所以某些名称被弄乱了。

1 个答案:

答案 0 :(得分:0)

由于this answer,我在%SOURCES%调用中将%LIBS%放在g++之前解决了这个问题。这似乎也有所不同:不仅库顺序很重要,输入(库之前的源)的顺序也很重要。