OpenGL/glut/stdc++ 构建错误

5

我正在使用Ubuntu,并尝试使用Synaptic安装所有包含“GLUT”以及SDL和OpenGL的软件。但是一个简单的程序仍然无法编译。它显示以下错误:

opengl1.cpp:(.text+0xe): 无法找到对 `glClear' 的引用
opengl1.cpp:(.text+0x1a): 无法找到对 `glBegin' 的引用
opengl1.cpp:(.text+0x2e): 无法找到对 `glVertex2i' 的引用
opengl1.cpp:(.text+0x33): 无法找到对 `glEnd' 的引用
opengl1.cpp:(.text+0x38): 无法找到对 `glFlush' 的引用
/tmp/ccnwQeLu.o: 在函数 `MyInit()':
opengl1.cpp:(.text+0x4c): 无法找到对 `glGetString' 的引用
opengl1.cpp:(.text+0x57): 无法找到对 `std::cout' 的引用
opengl1.cpp:(.text+0x5c): 无法找到对 `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' 的引用
opengl1.cpp:(.text+0x6c): 无法找到对 `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' 的引用
opengl1.cpp:(.text+0x78): 无法找到对 `glGetString' 的引用
opengl1.cpp:(.text+0x83): 无法找到对 `std::cout' 的引用
opengl1.cpp:(.text+0x88): 无法找到对 `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' 的引用
opengl1.cpp:(.text+0x98): 无法找到对 `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' 的引用
opengl1.cpp:(.text+0xc0): 无法找到对 `glClearColor' 的引用
opengl1.cpp:(.text+0xdf): 无法找到对 `glColor3f' 的引用
opengl1.cpp:(.text+0xec): 无法找到对 `glPointSize' 的引用
opengl1.cpp:(.text+0xf8): 无法找到对 `glMatrixMode' 的引用
opengl1.cpp:(.text+0xfd): 无法找到对 `glLoadIdentity' 的引用
opengl1.cpp:(.text+0x12d): 无法找到对 `gluOrtho2D' 的引用
/tmp/ccnwQeLu.o: 在函数 `main':
opengl1.cpp:(.text+0x14a): 无法找到对 `glutInit' 的引用
opengl1.cpp:(.text+0x156): 无法找到对 `glutInitDisplayMode' 的引用
opengl1.cpp:(.text+0x16d): 无法找到对 `glutInitWindowSize' 的引用
opengl1.cpp:(.text+0x181): 无法找到对 `glutInitWindowPosition' 的引用
opengl1.cpp:(.text+0x18d): 无法找到对 `glutCreateWindow' 的引用
opengl1.cpp:(.text+0x19e): 无法找到对 `glutDisplayFunc' 的引用
opengl1.cpp:(.text+0x1a3): 无法找到对 `glutMainLoop' 的引用
/tmp/ccnwQeLu.o: 在函数 `__static_initialization_and_destruction_0(int, int)':
opengl1.cpp:(.text+0x1cb): 无法找到对 `std::ios_base::Init::Init()' 的引用
opengl1.cpp:(.text+0x1d0): 无法找到对 `std::ios_base::Init::~Init()' 的引用
/tmp/ccnwQeLu.o:(.eh_frame+0x12): 无法找到对 `__gxx_personality_v0' 的引用
collect2: ld 返回 1 退出状态
如果我使用g ++而不是gcc,我会得到这个:
vim opebgl1.cpp
g ++ opengl1.cpp -o opengl1 -lGL -lstdc ++ -lc -lm
然后得到这个:
/tmp/ccCJBuIl.o:在函数“MyInit()”中:
opengl1.cpp:(.text + 0x12d):对“gluOrtho2D”的未定义引用
/tmp/ccCJBuIl.o:在函数“main”中:
opengl1.cpp:(.text + 0x14a):对“glutInit”的未定义引用
opengl1.cpp:(.text + 0x156):对“glutInitDisplayMode”的未定义引用
opengl1.cpp:(.text + 0x16d):对“glutInitWindowSize”的未定义引用
opengl1.cpp:(.text + 0x181):对“glutInitWindowPosition”的未定义引用
opengl1.cpp:(.text + 0x18d):对“glutCreateWindow”的未定义引用
opengl1.cpp:(.text + 0x19e):对“glutDisplayFunc”的未定义引用
opengl1.cpp:(.text + 0x1a3):对“glutMainLoop”的未定义引用
collect2:ld返回1个退出状态
那么,在Ubuntu中真正需要开始使用OpenGL的是什么?

在构建应用程序时,您是否链接了所有适当的库? - Troubadour
我包含了这些头文件:#include <X11/Xlib.h>和#include <GL/glut.h>。 - Dayanand shetake
另外,您是使用g++还是gcc进行构建? - Troubadour
包含头文件只是你需要做的一半。这允许程序编译但不链接。最终创建应用程序(或库)的命令应链接到您使用的库,否则它将不知道从哪里获取所有这些符号。发布您的构建命令,我们可以给您一些指针。 - Troubadour
使用 g++ 来编译 C++ 源代码。这样你会发现与 std::cout 相关的错误消失了。g++ 实际上只是一个围绕着 gcc 的包装器,可以自动为您链接标准的 C++ 库。如果您使用 gcc,则必须手动链接。使用 g++ 更加简单方便。 - Troubadour
@user553492:希望你不介意我编辑你的问题。有人标记它为离题,可能是因为你将它描述成了一个Ubuntu安装问题。当然,结果证明这是一个编程问题,所以我只是稍微澄清了一下。 - Troubadour
4个回答

8
将“-lstdc++ -lGL”添加到您的链接器标志中。 或者尝试按照以下方式编译:
g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm

(edit: added -lGLU, removed -lstdc++)


现在我收到了以下信息: /tmp/ccCJBuIl.o: 在函数 MyInit()' 中: opengl1.cpp:(.text + 0x12d):未定义参考 gluOrtho2D' /tmp/ccCJBuIl.o: 在函数 main' 中: opengl1.cpp:(.text + 0x14a):未定义参考 glutInit' opengl1.cpp:(.text + 0x156):未定义参考 glutInitDisplayMode' opengl1.cpp:(.text + 0x16d):未定义参考 opengl1.cpp:(.text + 0x181):未定义参考 glutInitWindowPosition' opengl1.cpp:(.text + 0x19e):未定义参考 glutDisplayFunc' opengl1.cpp:(.text + 0x1a3):未定义参考 glutMainLoop' collect2:ld 返回 1 退出状态 - Dayanand shetake
1
@user: 在-lGL的左侧添加-lglut和-lGLU。 - Drew Hall
如果您正在使用g ++,为什么需要-lstdc ++?问题似乎在于@user553492错误地使用了gcc - Troubadour
@pelya:当您使用g++进行编译/链接时(除非您明确禁用它),-lstdc++是隐含的。 - Drew Hall
错误似乎已经消失了,使用g++。现在唯一的问题是,程序编译成功,但运行时显示命令未找到。 - Dayanand shetake
@user:你的路径中可能没有“.”(当前目录)。只需像这样运行它:./opengl1 - Drew Hall

3
你需要将其与glut和GLU链接起来: ```

你需要将其与glut和GLU链接起来:

```
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU

.cpp结尾的文件必须使用g++编译。其余错误与链接过程有关,如果您使用我上面建议的命令构建应用程序,则不应发生这些错误。如果出现这些错误,请确保已安装libglutlibglu


2

请确保使用g++编译你的C++文件,以避免与C++标准库相关的链接器错误。

为了解决GLUT符号问题,你可能只需要在最终构建命令中添加-lglut即可。


0

在编译代码时,您必须根据您的包含文件指定一些参数。 您已经使用了-lGL-lm等。 要使用gluOrtho2D函数,需要使用参数-lGLU, 对于其他以glut开头的函数,请使用-lglut


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接