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

Having trouble with GLFW library linking using a makefile

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

我的生成文件:

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

运行 this 然而给出了来自 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 有headers 的内容。

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)

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

答案是 link 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() 工作的问题,但这可能是一个单独的问题

祝其他遇到此问题的人好运!