简体   繁体   English

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

[英]Having trouble with GLFW library linking using a makefile

I've been working on learning OpenGL on a windows machine running MinGW and I been able to compile and run an OpenGL program using the standard includes and glu32.dll and etc. This time I'm trying to compile a basic shader which seemed to be simple enough if I used GLFW, GLAD, GLM, and their respective dependencies. 我一直在运行MinGW的Windows机器上学习OpenGL,并且能够使用标准include和glu32.dll等来编译和运行OpenGL程序。这一次,我试图编译一个基本的着色器,如果我使用GLFW,GLAD,GLM及其各自的依赖关系,则非常简单。 I think I have everything other than GLFW properly included. 我认为除了GLFW之外,我还有其他所有内容。

My Makefile: 我的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

Running this however gives linking errors for functions from GLFW 但是, 运行此命令会导致来自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>

From reading about other people issues on this matter it seems to be a problem with the compiler not finding libglfw3.a and libglfw3dll.a which i placed in the same directory as all the other dlls. 通过阅读有关此问题的其他人的问题,似乎编译器没有找到与所有其他dll放在同一目录中的libglfw3.a和libglfw3dll.a就是一个问题。

This is the contents of my directory , with src having my main.cpp and deps having the headers for the includes. 这是目录内容 ,其中src具有main.cpp,而deps具有include头。

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>

Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

Update: I have tried different orders with linked libraries but they have not had any effect. 更新:我尝试过使用链接库执行不同的命令,但是它们没有任何效果。 However, interestingly enough removing libglfw3.a and libglfw3dll.a allows the program to compile only to then crash instantly. 但是,有趣的是,删除libglfw3.alibglfw3dll.a允许程序进行编译,然后立即崩溃。 Debugging with gdb yields the following 使用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)

Turns out the answer was simple. 原来答案很简单。 I discovered that I could open libglfw3dll.a as an archive and saw it was filled with #####.o files which defined the glfw functions. 我发现我可以打开libglfw3dll.a作为存档,并看到其中充满了定义glfw函数的#####。o文件。 Extracting this and placing it with makefile to let it complile them alongside my .o files removed this error but had strange linking issues when executed. 提取并使用makefile使其与我的.o文件一起编译,可以消除此错误,但在执行时会出现奇怪的链接问题。

The answer was to link libglfw3dll.a alongside the .o files as if it were a source. 答案是将libglfw3dll.a与.o文件链接起来,就好像它是源文件一样。 meaning my makefile became 意味着我的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

This compiled and executed fine although now I'm running into an issue getting glfwCreateWindow() working but that is probably a separate issue 尽管现在我遇到了使glfwCreateWindow()工作的问题, glfwCreateWindow()已编译并执行得很好,但这可能是一个单独的问题

Best of luck to anyone else who runs into this! 碰到这一切的人,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM