OpenGL - 使用 g++ 链接 glew 和 glut 库和 dll

Posted

技术标签:

【中文标题】OpenGL - 使用 g++ 链接 glew 和 glut 库和 dll【英文标题】:OpenGL - linking glew and glut libs and dlls with g++ 【发布时间】:2020-05-17 15:25:16 【问题描述】:

我确实花了 5 个小时试图让 OpenGl 代码用 g++ 编译。

我尝试了很多东西,我什至不记得在这里发帖了。

我想我的问题是将 .libs 和 .dlls 与 g++ 链接。

我已将 glew 和 glut 的所有库和 dll 放在名为 lib 的文件夹中。 我做的最后一件事是发出这个命令:

g++ -I./include mytest1.cpp -L./lib 

还是不行。有什么帮助吗?

编辑: 我在窗户上。我不认为我在使用 FreeGlut,文件名是 glut32.dll 和 glut32.lib

这是编译器错误:

C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x7): undefined
reference to `_imp____glewDeleteVertexArrays'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x22): undefined
 reference to `_imp____glewDeleteBuffers'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x51): undefined
 reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x6a): undefined
 reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x8b): undefined
 reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb4): undefined
 reference to `_imp____glewEnableVertexAttribArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xc7): undefined
 reference to `_imp____glewVertexAttribPointer'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x103): undefine
d reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x127): undefine
d reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x150): undefine
d reference to `_imp____glewEnableVertexAttribArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x163): undefine
d reference to `_imp____glewVertexAttribPointer'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x19f): undefine
d reference to `_imp____glewBindBuffer'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x1c3): undefine
d reference to `_imp____glewBufferData'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x206): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x221): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x265): undefine
d reference to `glDrawElements@16'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x26d): undefine
d reference to `_imp____glewBindVertexArray'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x28f): undefine
d reference to `glClear@4'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x2af): undefine
d reference to `glFlush@0'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x3e6): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x434): undefine
d reference to `glutPostRedisplay'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x581): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x5cf): undefine
d reference to `glutPostRedisplay'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x633): undefine
d reference to `glViewport@16'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x68e): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x718): undefine
d reference to `glClearColor@16'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x833): undefine
d reference to `_imp____glewGenVertexArrays'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x84e): undefine
d reference to `_imp____glewGenBuffers'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x906): undefine
d reference to `initshaders(unsigned int, char const*)'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x91f): undefine
d reference to `initshaders(unsigned int, char const*)'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x93b): undefine
d reference to `initprogram(unsigned int, unsigned int)'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x945): undefine
d reference to `_imp____glewGetUniformLocation'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x966): undefine
d reference to `_imp____glewGetUniformLocation'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x987): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0x9d5): undefine
d reference to `_imp____glewUniformMatrix4fv'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xa61): undefine
d reference to `glutInit'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xa6d): undefine
d reference to `glutInitDisplayMode'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xa81): undefine
d reference to `glutInitWindowSize'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xa95): undefine
d reference to `glutInitWindowPosition'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xaa1): undefine
d reference to `glutCreateWindow'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xaa6): undefine
d reference to `_imp__glewInit@0'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xabc): undefine
d reference to `_imp__glewGetString@4'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb05): undefine
d reference to `glutDisplayFunc'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb11): undefine
d reference to `glutReshapeFunc'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb1d): undefine
d reference to `glutKeyboardFunc'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb29): undefine
d reference to `glutMouseFunc'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb35): undefine
d reference to `glutMotionFunc'
C:\Users\User1\AppData\Local\Temp\cc219WZV.o:mytest1.cpp:(.text+0xb3a): undefine
d reference to `glutMainLoop'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\U
ser1\AppData\Local\Temp\cc219WZV.o: bad reloc address 0x20 in section `.text$_ZS
t4sqrtf[__ZSt4sqrtf]'
collect2.exe: error: ld returned 1 exit status

EDIT2:lib 文件夹的内容:

EDIT3:

架构:64 位 x64

编译器细节(g++ -v):

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=m
ingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto
--enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++
,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-l
ibstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gm
p-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --
with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-
libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/
mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)

这些文件是从 OpenGL 的 edx 课程下载的。它使用了 Visual Studio,但我不能使用它,因为我的电脑很旧而且我有驱动程序问题,因此我试图从命令行编译所有内容

【问题讨论】:

添加一些有关您的设置的信息(操作系统和编译器版本,使用的“过剩”等)会有所帮助。从文本中我们可能只会猜测您使用的是 Windows 系统并使用 MinGW 或 msys/msys2 工具链,但您也可能使用 Cygwin。 GCC 的库文件格式是“.a”,而不是“.lib”,因此您可能需要编译 FreeGLUT 和/或 GLEW 或使用“dlltool”生成 GCC 的导出文件。 我将编辑问题。感谢您的宝贵时间。 【参考方案1】:

您告诉 GCC 在特定文件夹中查找库和头文件,但您没有告诉 GCC 将库实际链接到输出二进制文件(想象一下“lib”文件夹中有几千兆字节的编译库。 GCC 或任何其他 C/C++ 编译器不会尝试链接所有库,因为其中一些甚至可能不兼容或包含相同代码的多个副本。

至少你应该运行

g++ -I./include mytest1.cpp -L./lib -lopengl32 -lglew -lfreeglut -lgdi32 -luser32

在这里,我假设您正在使用或多或少现代版本的 FreeGLUT。

“-lSomeLib”假设您的库文件夹中有“libSomeLib.a”文件。 例如,“-lopengl32”尝试链接 libopengl32.a 文件(这是一个系统库,至少在 MinGW 包中)。 gdi32 和 user32 只是 WinAPI 程序必需的库。

有一个dlltool mini HowTo 解释了如何从“glew32.dll”创建“libglew.a”文件,但关于“glut32”我想你有问题,因为这可能与您的 GCC,因此您必须在 Google 上搜索 FreeGLUT 作为替代方案(旧的 GLUT 不支持您尝试与 GLEW 一起使用的现代扩展)。

总结所需的步骤:

    获取正确的 FreeGLUT 库源或预编译的 64 位 (!) .dll

    获取 GLEW 的源代码(它是一个小型库,您可以逐个文件编译它的源代码)或下载 GLEW 的 64 位版本(我认为您的预编译 glew 不是 64 位的,您可以使用来自 ntcore.com 的 CFF Explorer 工具进行检查)。然后编译 GLEW 或为您的 .dll 生成导出 .a 文件。

    拥有 libglew.a 和 libfreeglut.a 文件后,添加 -lglew 和 -lfreeglut。

我知道这是一个很长的路要走,但最终它奏效了。祝你好运。

【讨论】:

c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:无法找到d -lglew c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:找不到 d -lfreeglut collect2。 exe: 错误: ld 返回 1 个退出状态 那么,“lib”文件夹的具体内容是什么?您可以将文件列表(如果不是那么大)附加到问题中。 另一件事是你从哪里得到的 lib 文件?问题现在很明显 - 您没有 GLEW 导出库文件(我的意思是,您的 GCC 编译器的有效库文件)。顺便说一句,目标架构是什么。是 64 位 x64 还是 32 位 x86 ? 'gcc -v' 可以告诉你编译器的细节。这也应该附在问题上。 我将其添加为图片 您提出的所有问题都会添加到问题中。非常感谢您的宝贵时间!

以上是关于OpenGL - 使用 g++ 链接 glew 和 glut 库和 dll的主要内容,如果未能解决你的问题,请参考以下文章

链接GLEW等,_glViewport是唯一未解决的

从 DLL 链接到 OpenGL:运行时 glew 函数为空

glew和glfw的opengl初始化问题

无法使用 GLEW (mac,c++) 在 openGL 中绘制任何内容

Qt Creator 中的 Glew 链接问题?

Opengl ES Glew库 ----- By YDD的铁皮锅