GLFW MinGW链接错误

18

我一直在尝试使用C++测试GLFW,但是遇到了不断的链接器问题。虽然我有Java和C#的经验,但是直接与编译器打交道还是有些陌生。以下是我的设置信息。

IDE:Qt Creator

操作系统:Windows 7 64位

编译器:MinGW32 4.8.1

01:23:26: Starting: "C:\MinGW\bin\mingw32-make.exe" 
C:/MinGW/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'A:/workspace_cpp/Test-Debug'
g++ -Wl,-subsystem,console -mthreads -o debug\Test.exe debug/main.o  -lglfw3 -lopengl32 
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.::(.text+0x2c7): undefined reference to `CreateDCW@16'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x358): undefined reference to `GetDeviceCaps@8'
Makefile.Debug:77: recipe for target 'debug\Test.exe' failed
mingw32-make[1]: Leaving directory 'A:/workspace_cpp/Test-Debug'
Makefile:34: recipe for target 'debug' failed
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x370): undefined reference to `GetDeviceCaps@8'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor .c:(.text+0x39e): undefined reference to `DeleteDC@4'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:     c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj): bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

我正在测试的代码是GLFW文档页面上的代码,我正在使用自己构建的GLFW版本,并且已经尝试过这个解决方法和其他几个潜在的解决方案。我已经尝试使用预编译的GLFW mingw库,但无法使它们正常工作。


4
看起来你漏掉了一个库。CreateDCWGetDeviceCapsDeleteDC都存在于gdi32.dll中,你需要至少添加gdi32导入库。只需按照添加glfw3opengl32库的方式添加即可。 - enhzflep
非常感谢!我已经苦苦挣扎了很久,我不知道gdi32库的存在,也没有在任何地方提到过。我不知道如何给你的评论点赞或选择最佳答案,但你真的帮了我大忙! - Matthew D
不客气。我有旧版本的“Win32API.hlp”和“win32sdk.hlp”。在每个文件中,都有一个标记为“快速信息”的按钮,其中列出了(a)声明函数的.h文件和(b)包含实际使用dll文件所需代码的.lib(或gcc情况下的.a)文件。我发现它们是无价之宝。如果您找不到在线帮助,需要离线帮助,请给我发送电子邮件,我会向您转发其中一个。我的电子邮件地址在我的个人资料页面上。 - enhzflep
当问题已经解决时,请不要将“已解决”或类似的内容添加到标题中。只需发布一个答案并将其标记为已接受即可(或让@enhzflep发布答案)。这对那些可能阅读此文并对解决方案感兴趣的其他人更有帮助。 - jalf
我的错误,我已经把对我有用的解决方案发布为答案了。 - Matthew D
1个回答

30

enhzflep提供的解决方案是在编译时包含gdi32库,使得所有必要的链接器为-lglfw3 -lgdi32 -lopengl32。缺失的方法CreateDCWGetDeviceCapsDeleteDC都在MinGW库文件夹中的C:\MinGW\lib\libgdi32.a中。


必须在我的构建命令末尾添加这些-lglfw3 -lgdi32,放在-o和main.cpp文件之后,因为链接必须在mingw的末尾。 - abelito

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接