使用 makefile 链接 GLFW 库时遇到问题

Posted

技术标签:

【中文标题】使用 makefile 链接 GLFW 库时遇到问题【英文标题】:Having trouble with GLFW library linking using a makefile 【发布时间】:2018-11-29 20:22:05 【问题描述】:

我一直致力于在运行 MinGW 的 Windows 机器上学习 OpenGL,我能够使用标准包含和 glu32.dll 等编译和运行 OpenGL 程序。这次我试图编译一个基本着色器如果我使用 GLFW、GLAD、GLM 和它们各自的依赖项,这似乎很简单。我认为除了 GLFW 之外的所有内容都已正确包含在内。

我的 Makefile:

all:
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c deps/glad/*.c
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/common/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/mesh/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/*.cpp
    g++ -Wall -g *.o -o run$(EXEEXT) -L ./ -lgdi32 -lglut32 -lglu32 -lopengl32 -lglfw3
    make clean

clean:
    cmd.exe /c del *.o

运行这个但是会给出来自 GLFW 的函数的链接错误

C:\Users\plore\Desktop\app>make
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c deps/glad/*.c
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/common/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/mesh/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/*.cpp
    In file included from c:\users\plore\desktop\app\deps\glm\detail/func_exponential_simd.inl:4:0,
                     from c:\users\plore\desktop\app\deps\glm\detail/func_exponential.inl:150,
                     from c:\users\plore\desktop\app\deps\glm\exponential.hpp:110,
                     from c:\users\plore\desktop\app\deps\glm\detail/func_geometric.inl:1,
                     from c:\users\plore\desktop\app\deps\glm\geometric.hpp:116,
                     from c:\users\plore\desktop\app\deps\glm\detail/func_matrix.inl:1,
                     from c:\users\plore\desktop\app\deps\glm\matrix.hpp:161,
                     from src/shader/../../deps/glm/./ext/../detail/type_mat2x2.inl:1,
                     from src/shader/../../deps/glm/./ext/../detail/type_mat2x2.hpp:176,
                     from src/shader/../../deps/glm/./ext/matrix_double2x2.hpp:5,
                     from src/shader/../../deps/glm/mat2x2.hpp:5,
                     from src/shader/../../deps/glm/glm.hpp:119,
                     from src/shader/shader_s.h:15,
                     from src/main.cpp:20:
    c:\users\plore\desktop\app\deps\glm\simd\exponential.h: In function 'glm_f32vec4 glm_vec1_sqrt_lowp(glm_f32vec4)':
    c:\users\plore\desktop\app\deps\glm\simd\exponential.h:10:64: warning: SSE vector return without SSE enabled changes the ABI [-Wpsabi]
     GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_sqrt_lowp(glm_f32vec4 x)
                                                                    ^
    g++ -Wall -g *.o -o run -L ./ -lgdi32 -lglut32 -lglu32 -lopengl32 -lglfw3
    Warning: resolving ___glutInitWithExit@12 by linking to ___glutInitWithExit
    Use --enable-stdcall-fixup to disable these warnings
    Use --disable-stdcall-fixup to disable these fixups
    Warning: resolving ___glutCreateWindowWithExit@8 by linking to ___glutCreateWindowWithExit
    Warning: resolving ___glutCreateMenuWithExit@8 by linking to ___glutCreateMenuWithExit
    Warning: resolving _glPushMatrix@0 by linking to _glPushMatrix
    Warning: resolving _glTranslatef@12 by linking to _glTranslatef
    Warning: resolving _glRotatef@16 by linking to _glRotatef
    Warning: resolving _glPopMatrix@0 by linking to _glPopMatrix
    Warning: resolving _glColor3f@12 by linking to _glColor3f
    Warning: resolving _glBegin@4 by linking to _glBegin
    Warning: resolving _glVertex2f@8 by linking to _glVertex2f
    Warning: resolving _glEnd@0 by linking to _glEnd
    main.o: In function `Z12processInputP10GLFWwindow':
    C:\Users\plore\Desktop\app/src/main.cpp:35: undefined reference to `_imp__glfwGetKey'
    C:\Users\plore\Desktop\app/src/main.cpp:36: undefined reference to `_imp__glfwSetWindowShouldClose'
    main.o: In function `main':
    C:\Users\plore\Desktop\app/src/main.cpp:121: undefined reference to `_imp__glfwInit'
    C:\Users\plore\Desktop\app/src/main.cpp:122: undefined reference to `_imp__glfwWindowHint'
    C:\Users\plore\Desktop\app/src/main.cpp:123: undefined reference to `_imp__glfwWindowHint'
    C:\Users\plore\Desktop\app/src/main.cpp:124: undefined reference to `_imp__glfwWindowHint'
    C:\Users\plore\Desktop\app/src/main.cpp:132: undefined reference to `_imp__glfwCreateWindow'
    C:\Users\plore\Desktop\app/src/main.cpp:136: undefined reference to `_imp__glfwTerminate'
    C:\Users\plore\Desktop\app/src/main.cpp:139: undefined reference to `_imp__glfwMakeContextCurrent'
    C:\Users\plore\Desktop\app/src/main.cpp:140: undefined reference to `_imp__glfwSetFramebufferSizeCallback'
    C:\Users\plore\Desktop\app/src/main.cpp:144: undefined reference to `_imp__glfwGetProcAddress'
    C:\Users\plore\Desktop\app/src/main.cpp:255: undefined reference to `_imp__glfwWindowShouldClose'
    C:\Users\plore\Desktop\app/src/main.cpp:279: undefined reference to `_imp__glfwSwapBuffers'
    C:\Users\plore\Desktop\app/src/main.cpp:280: undefined reference to `_imp__glfwPollEvents'
    C:\Users\plore\Desktop\app/src/main.cpp:291: undefined reference to `_imp__glfwTerminate'
    collect2.exe: error: ld returned 1 exit status
    Makefile:2: recipe for target 'all' failed
    make: *** [all] Error 1

    C:\Users\plore\Desktop\app>

从阅读其他人关于这个问题的问题来看,编译器似乎没有找到 libglfw3.a 和 libglfw3dll.a 的问题,我将它们放在与所有其他 dll 相同的目录中。

这是我的目录的内容,src 有我的 main.cpp,deps 有包含头文件。

C:\Users\plore\Desktop\app>dir
 Volume in drive C has no label.
 Volume Serial Number is 9CEF-2A78

 Directory of C:\Users\plore\Desktop\app

11/29/2018  07:46 PM    <DIR>          .
11/29/2018  07:46 PM    <DIR>          ..
11/29/2018  07:39 PM    <DIR>          deps
08/18/2016  06:04 AM           238,974 glfw3.dll
11/27/2018  04:26 PM           245,760 glu32.dll
11/27/2018  04:26 PM           237,568 glut32.dll
08/18/2016  06:05 AM           150,452 libglfw3.a
08/18/2016  06:05 AM            65,788 libglfw3dll.a
11/29/2018  07:43 PM               515 Makefile
11/27/2018  04:26 PM           757,248 opengl32.dll
11/27/2018  04:26 PM                55 README.md
11/29/2018  12:10 PM    <DIR>          src
               8 File(s)      1,696,360 bytes
               4 Dir(s)  27,734,446,080 bytes free

C:\Users\plore\Desktop\app>

任何帮助将不胜感激,谢谢。

更新: 我尝试过使用链接库的不同命令,但它们没有任何效果。然而,有趣的是,删除libglfw3.alibglfw3dll.a 只允许程序编译然后立即崩溃。使用 gdb 进行调试会产生以下结果

C:\Users\plore\Desktop\app>gdb
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file run.exe
Reading symbols from C:\Users\plore\Desktop\app\run.exe...done.
(gdb) r
Starting program: C:\Users\plore\Desktop\app/run.exe
[New Thread 13120.0x3c3c]
[New Thread 13120.0x38a8]
[New Thread 13120.0x2b08]
[New Thread 13120.0x37bc]
[New Thread 13120.0x828]
[New Thread 13120.0x2810]
[New Thread 13120.0x39d0]
[New Thread 13120.0x3c34]
[New Thread 13120.0x1610]
[New Thread 13120.0x268c]
[New Thread 13120.0x3600]
[New Thread 13120.0x319c]
[New Thread 13120.0x34d8]
[New Thread 13120.0x15ec]

Program received signal SIGSEGV, Segmentation fault.
0x692b1d25 in _glfwRefreshContextAttribs () from C:\Users\plore\Desktop\app\glfw3.dll
(gdb) where
#0  0x692b1d25 in _glfwRefreshContextAttribs () from C:\Users\plore\Desktop\app\glfw3.dll
#1  0x63f1f3ff in wglSwapLayerBuffers () from C:\Users\plore\Desktop\app\opengl32.dll
#2  0x00003c3c in ?? ()
#3  0x63f1f632 in wglSwapLayerBuffers () from C:\Users\plore\Desktop\app\opengl32.dll
#4  0x36384224 in ?? ()
#5  0x0068fcf8 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)

【问题讨论】:

libglfw3.alibglfw3dll.a 不是 dll,如果链接器没有找到它们,则会发出相应的错误。最有可能的问题是库的顺序和使用-L 而不是-lglfw3 哦,哇,我用 -L 代替 -l 真是太愚蠢了。我已经在图书馆订购了一些,但没有成功。可悲的是,修复这个错字实际上并没有改变任何东西 【参考方案1】:

原来答案很简单。我发现我可以将 libglfw3dll.a 作为存档打开,并看到其中充满了定义 glfw 函数的#####.o 文件。将其提取并与 makefile 一起放置,使其与我的 .o 文件一起编译,消除了此错误,但在执行时出现了奇怪的链接问题。

答案是将 libglfw3dll.a 与 .o 文件一起链接,就好像它是一个源一样。意味着我的makefile变成了

all:
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c deps/glad/*.c
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/common/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/objects/mesh/*.cpp
    g++ -Wall -Wno-unknown-pragmas -Wno-unused-function -g -c src/*.cpp
    g++ -Wall -g *.o *.a -o run$(EXEEXT) -L ./ -lgdi32 -lglut32 -lglu32 -lopengl32 -lglfw3
    make clean

clean:
    cmd.exe /c del *.o

虽然现在我遇到了让glfwCreateWindow() 工作的问题,但这编译和执行都很好,但这可能是一个单独的问题

祝遇到这种情况的其他人好运!

【讨论】:

以上是关于使用 makefile 链接 GLFW 库时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

GLFW 和代码块

怎么编写Makefile生成静态库

与 GLFW MinGW 的链接问题

GLFW 链接,未定义对 init 的引用

C ++ glfw未解决的外部符号错误[重复]

如何防止我的makefile由于静态库而重新链接