opengl - G++。无法链接库

标签 opengl compiler-errors compilation linker g++

我正在尝试链接库以编译一些OpenGL代码。

我有尝试编译mytest1.cpp的源代码,并且在同一目录中,我有一个名为include的文件夹和头文件,还有一个名为lib的文件夹和所有库。

这是名为lib的文件夹内的内容:
enter image description here

因此,当我运行此命令时,为了将所有内容链接在一起:

g++ -I.\include\ mytest1.cpp -L.\lib\ -llibfreeglut -lopengl32 -lglew32

我收到此错误:
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot fin
d -llibfreeglut
collect2.exe: error: ld returned 1 exit status

作业系统:Windows x64

编辑:我遵循了迈克·金汉的指示,但不幸的是,这就是我得到的
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x75): undefined
 reference to `_imp____glewDeleteVertexArrays'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x90): undefined
 reference to `_imp____glewDeleteBuffers'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xc0): undefined
 reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xd9): undefined
 reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xfa): undefined
 reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x123): undefine
d reference to `_imp____glewEnableVertexAttribArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x136): undefine
d reference to `_imp____glewVertexAttribPointer'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x171): undefine
d reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x195): undefine
d reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x1be): undefine
d reference to `_imp____glewEnableVertexAttribArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x1d1): undefine
d reference to `_imp____glewVertexAttribPointer'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x20c): undefine
d reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x230): undefine
d reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x273): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x28f): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x2db): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x4d4): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x6e8): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0x859): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xaf1): undefine
d reference to `_imp____glewGenVertexArrays'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xb0c): undefine
d reference to `_imp____glewGenBuffers'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xbbe): undefine
d reference to `initshaders(unsigned int, char const*)'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xbd7): undefine
d reference to `initshaders(unsigned int, char const*)'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xbf3): undefine
d reference to `initprogram(unsigned int, unsigned int)'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xbfd): undefine
d reference to `_imp____glewGetUniformLocation'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xc1e): undefine
d reference to `_imp____glewGetUniformLocation'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xc3f): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\ccyjpOjl.o:mytest1.cpp:(.text+0xc8e): undefine
d reference to `_imp____glewUniformMatrix4fv'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: C:\Users\U
ser1\AppData\Local\Temp\ccyjpOjl.o: bad reloc address 0xd in section `.text$_ZSt
4sqrtf[__ZSt4sqrtf]'
collect2.exe: error: ld returned 1 exit status

最佳答案

也许您已经成功实现了以下功能:

g++ ... -L.\lib\ ... -lglew32

使用.\lib\glew32.lib,现在您想知道为什么会这样:
g++ ... -L.\lib\ ... -llibfreeglut

找不到.\lib\libfreeglut.a
在这里,您可以使用Windows端口的
Unix/Linux工具链(GCCGNU binutils)。

通常,在* nix系统上,提供name库进行链接的文件是
称为libname.a(静态库)或libname.so(动态库,也称为共享库)。

选项-lname指示GNU链接器搜索指定的和
文件libname.alibname.so的默认库目录
并将找到的第一个输入到链接。如果找到相同的两者
搜索目录,然后选择libname.so
但是此协议(protocol)不适用于工具链的Windows端口-例如您的mingw端口-因为在Windows上,本机传统是调用静态库name.lib和动态库name.dll,而动态库始终具有
扩展名.dll
因此,通过搜索您的mingw GNU链接器,使其遵循-lname任何文件:
libname.a libname.dll name.lib name.dll

(如果它像我的一样,则首选*.lib而不是*.a,并且首选*.a而不是*.dll如果在同一搜索目录中找到多个候选人)。

因此,您的链接:
g++ -I.\include\ mytest1.cpp -L.\lib\ -llibfreeglut -lopengl32 -lglew32

将成功遵守-lglew32,因为glew32.lib中存在.\lib
但这将在-llibfreeglut上失败,原因是:
liblibfreeglut.a liblibfreeglut.dll libfreeglut.lib libfreeglut.dll

存在。由于libfreeglut.a确实存在,请使用:
g++ -I.\include\ mytest1.cpp -L.\lib\ -lfreeglut -lopengl32 -lglew32

解决此特定的链接失败。您没有告诉我们您的期望
链接器以找到满足-lopengl32的文件。

稍后...新的麻烦

建立链接以查找库.\lib\libfreeglut.a您现在对于许多引用都有未定义的引用链接错误
从您自己的代码_imp____glew*中匹配mytest1.cpp。显然没有
您对glew库的代码的预期引用已得到解决。

使用Windows .lib.dll文件时,您需要了解的一件事是name.lib文件可能是一个自包含的静态库(与
存档格式(例如* nix name.a静态库),或者它可能是专门的
实际上充当DLL import library的文件
在链接时代表DLL的代理,后者是纯粹的Microsoft
理念。 DLL导入库name.lib定义了_imp_<symbol> 1形式的符号,其在程序中的链接使
操作系统加载程序为程序提供了指向<symbol>的DLL定义的运行时地址的指针,
name.dll加载到进程地址空间中时。

我有把握地猜测您的glew32.lib实际上不是静态库
您或其他人使用mingw GCC 4.8.1或ABI compatible compiler从源构建的
但它是您下载的二进制glew包中的DLL导入库
是使用Microsoft编译器构建的,用于Microsoft编译器。

通常,您不能在同一链接中混合目标文件和/或库
由不同的非ABI兼容编译器构建的,而MS C/C++不是ABI
与GCC C/C++兼容。

名称处理协议(protocol)是ABI的一部分。的名称处理协议(protocol)
MS C++和GCC/Clang C++根本不同并且尝试进行链接
混合这两者总是会因未定义的引用错误而失败。名字改头换面
MS C和GCC/Clang C的协议(protocol)略有不同,但它们确实有所不同,
而您的联系现在违反了他们的分歧之一,试图
匹配由mingw g++从C头文件<GL/*.h>生成的C符号
当它用MS C生成的C符号编译mytest1.cpp
它建立了你的glew32.lib。例如,您的编译器输出未定义的
引用
_imp____glewBindVertexArray

链接只能解析为glew32.lib中定义的符号,但是
如果您运行:
>objdump -t glew32.lib | findstr "glewBindVertexArray"

mingw控制台中,最接近的是:
__imp___glewBindVertexArray

缺少一个_

您需要构建glew-一个静态库或DLL,或两者-
使用mingw工具链从源代码开始,然后编译mytest1.cpp(和
任何后续的GLEW程序),使用您构建的库的头文件
将程序链接到已构建的库。

您会在互联网上找到大量有关“为明格建树”的指南,
Building glew on windows with mingw的答案中特别是在Stackoverflow上

[1]由于C或C++编译器,<symbol>本身可能具有一个或多个前导_ name-mangling以及可能的源代码命名约定。

关于opengl - G++。无法链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61868309/

相关文章:

python - 如何使用 Python 和 PyGame 在 OpenGL 上显示 2D 形状

c++ - C++ 中的 "Undefined symbols"错误

c - C中的可变长度数组,它们是如何编译的

templates - 使用nvcc在CUDA中编译模板函数时出错

c# - 快速多窗口渲染

java - LibGDX 相机平滑翻译

c++ - 无法在 C++ 类中创建构造函数

java - 编译时出错?

scala - 可以编译Scala程序但不能运行它们

c++ - OpenGL 照明位置固定在不需要的位置