使用Makefile链接GLFW库时遇到麻烦

时间:2018-11-29 20:22:06

标签: c++ opengl makefile glfw

我一直在运行MinGW的Windows机器上学习OpenGL,并且能够使用标准的include和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>

从阅读有关此问题的其他人的问题来看,似乎编译器没有找到我与所有其他dll放在同一目录中的libglfw3.a和libglfw3dll.a就是一个问题。

这是目录的内容,其中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)

1 个答案:

答案 0 :(得分:0)

结果很简单。我发现我可以打开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()正常工作的问题,但这仍然是一个单独的问题

碰碰运气的人最幸运!