Opengl G++。无法链接库

Opengl G++。无法链接库,opengl,compiler-errors,compilation,linker,g++,Opengl,Compiler Errors,Compilation,Linker,G++,为了编译一些OpenGL代码,我正在尝试链接一个库 我有我试图编译mytest1.cpp的源代码,在同一个目录中,我得到了一个名为include的文件夹,其中包含头文件,还有一个名为lib的文件夹,其中包含所有库 这是名为lib的文件夹中的内容: 因此,当我运行此命令时,为了将所有内容链接在一起: g++ -I.\include\ mytest1.cpp -L.\lib\ -llibfreeglut -lopengl32 -lglew32 我得到这个错误: c:/mingw/bin/../l

为了编译一些OpenGL代码,我正在尝试链接一个库

我有我试图编译mytest1.cpp的源代码,在同一个目录中,我得到了一个名为include的文件夹,其中包含头文件,还有一个名为lib的文件夹,其中包含所有库

这是名为lib的文件夹中的内容:

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

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工具链&

经典的on*nix系统是提供链接库名称的文件 称为libname.a静态库或libname.so动态库,也称为共享库

选项-lname指示GNU链接器搜索指定的 libname.a或libname.so文件的默认库目录 并将它找到的第一个输入到链接。如果它发现两者在同一个 搜索目录,然后选择libname.so

但是这个协议不适用于工具链的Windows端口,比如您的 mingw端口-因为在Windows上,本机传统是调用静态库 name.lib和动态库name.dll,并且动态库始终具有 扩展名为.dll

因此,您的mingw GNU链接器通过搜索 任何文件:

libname.a libname.dll name.lib name.dll
如果它和我的一样,它更喜欢*.lib而不是*.a,更喜欢*.a而不是*.dll 如果在同一搜索目录中找到多个候选项

因此,您的链接:

g++ -I.\include\ mytest1.cpp -L.\lib\ -llibfreeglut -lopengl32 -lglew32
将成功遵守-lglew32,因为在。\lib中存在glew32.lib。 但它将在-Llibreeglut上失败,因为:

liblibfreeglut.a liblibfreeglut.dll libfreeglut.lib libfreeglut.dll
存在。由于libfreeglut.a确实存在,请使用:

g++ -I.\include\ mytest1.cpp -L.\lib\ -lfreeglut -lopengl32 -lglew32
以解决此特定的连杆故障。你没有告诉我们你希望去哪里 查找文件的链接器-lopengl32

后来。。。新麻烦

已获得查找库的链接。\lib\libfreeglut.a 现在,许多引用都存在未定义的引用链接错误 从您自己的代码mytest1.cpp中匹配。显然没有 正在解析代码对glew库的预期引用

在使用Windows.lib和.dll文件时,需要知道的一件事是 name.lib文件可能是一个完全相同的自包含静态库 存档格式为*nix名称。一个静态库,或者它可能是专用的 实际用作 它代表linktime中DLL的代理,后者是纯Microsoft的 主意DLL import library name.lib定义形式为_imp_1的符号,其在程序中的链接允许 OS loader为程序提供指向DLL定义的运行时地址的指针, 将name.dll加载到进程地址空间时

我自信地猜测您的glew32.lib实际上不是一个静态库 您或其他人使用mingw GCC 4.8.1或 但您下载的二进制glew包中是否有DLL导入库 它是用Microsoft编译器构建的,用于Microsoft编译器

通常,不能在同一链接中混合使用对象文件和/或库 它们是由不同的不兼容ABI的编译器构建的,MS C/C++不是ABI 与GCC/C++兼容

名称篡改协议是ABI的一部分。的名称 C++和GCC/CLAN C++是根本不同的,并且尝试联系。 对于未定义的引用错误,这两者的混合总是失败的。这个名字被弄坏了 MS C和GCC/Clang C的协议有点分歧,但它们确实存在分歧, 你的联系现在与他们的分歧相冲突,试图 匹配mingw g++从C头文件生成的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程序,以及 将您的程序与您构建的库相链接

你会在互联网上找到很多关于为mingw构建glew的指南, 值得注意的是,在对

[1 ]由于C或C++编译器的结果,可能本身有一个或多个前导
还有可能是源代码命名约定。

我看到了反对票,但没有解决方案。如果这么简单,那你为什么不提供一个解决方案呢
投反对票?这几天你发布的所有内容都会被否决。谢谢!我尝试了你的命令,但是我在编译器上遇到了太多的错误,它们无法放入注释中,所以我将编辑这个问题。因为我花了整整两天的时间来处理这个编译,但都没有成功,我想知道。。。我是否必须从源代码中重新编译FreeGLUT?@user1584421 Answer extended用于解决您的新问题。