C++ OpenGL/glut/stdc++;构建错误

C++ OpenGL/glut/stdc++;构建错误,c++,opengl,linker-errors,glut,C++,Opengl,Linker Errors,Glut,我正在使用Ubuntu,并尝试使用synaptic安装所有有“GLUT”字样的东西,还有SDL和opengl。但仍然有一个简单的程序无法编译。它表明: opengl1.cpp:(.text+0xe): undefined reference to `glClear' opengl1.cpp:(.text+0x1a): undefined reference to `glBegin' opengl1.cpp:(.text+0x2e): undefined reference to `glVerte

我正在使用Ubuntu,并尝试使用synaptic安装所有有“GLUT”字样的东西,还有SDL和opengl。但仍然有一个简单的程序无法编译。它表明:

opengl1.cpp:(.text+0xe): undefined reference to `glClear' opengl1.cpp:(.text+0x1a): undefined reference to `glBegin' opengl1.cpp:(.text+0x2e): undefined reference to `glVertex2i' opengl1.cpp:(.text+0x33): undefined reference to `glEnd' opengl1.cpp:(.text+0x38): undefined reference to `glFlush' /tmp/ccnwQeLu.o: In function `MyInit()': opengl1.cpp:(.text+0x4c): undefined reference to `glGetString' opengl1.cpp:(.text+0x57): undefined reference to `std::cout' opengl1.cpp:(.text+0x5c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' opengl1.cpp:(.text+0x6c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' opengl1.cpp:(.text+0x78): undefined reference to `glGetString' opengl1.cpp:(.text+0x83): undefined reference to `std::cout' opengl1.cpp:(.text+0x88): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' opengl1.cpp:(.text+0x98): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' opengl1.cpp:(.text+0xc0): undefined reference to `glClearColor' opengl1.cpp:(.text+0xdf): undefined reference to `glColor3f' opengl1.cpp:(.text+0xec): undefined reference to `glPointSize' opengl1.cpp:(.text+0xf8): undefined reference to `glMatrixMode' opengl1.cpp:(.text+0xfd): undefined reference to `glLoadIdentity' opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D' /tmp/ccnwQeLu.o: In function `main': opengl1.cpp:(.text+0x14a): undefined reference to `glutInit' opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize' opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition' opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow' opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc' opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop' /tmp/ccnwQeLu.o: In function `__static_initialization_and_destruction_0(int, int)': opengl1.cpp:(.text+0x1cb): undefined reference to `std::ios_base::Init::Init()' opengl1.cpp:(.text+0x1d0): undefined reference to `std::ios_base::Init::~Init()' /tmp/ccnwQeLu.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status 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):对“GLUTINITWindowsSize”的未定义引用 opengl1.cpp:(.text+0x181):对“glutInitWindowPosition”的未定义引用 opengl1.cpp:(.text+0x18d):对“glutCreateWindow”的未定义引用 opengl1.cpp:(.text+0x19e):对“glutDisplayFunc”的未定义引用 opengl1.cpp:(.text+0x1a3):对“glutMainLoop”的未定义引用 /tmp/ccnwQeLu.o:在函数“静态初始化”和“销毁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):未定义对“\uuuugxx\u personality\uv0”的引用 collect2:ld返回了1个退出状态 如果我使用g++而不是gcc,我会得到以下结果:

vim opebgl1.cpp g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm and then get this : /tmp/ccCJBuIl.o: In function `MyInit()': opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D' /tmp/ccCJBuIl.o: In function `main': opengl1.cpp:(.text+0x14a): undefined reference to `glutInit' opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize' opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition' opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow' opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc' opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop' collect2: ld returned 1 exit status vim opebgl1.cpp g++opengl1.cpp-o opengl1-lGL-lstdc++-lc-lm 然后得到这个: /tmp/ccCJBuIl.o:在函数'MyInit()'中: opengl1.cpp:(.text+0x12d):对“gluOrtho2D”的未定义引用 /tmp/ccCJBuIl.o:在“主”功能中: opengl1.cpp:(.text+0x14a):对“glutInit”的未定义引用 opengl1.cpp:(.text+0x156):对“glutInitDisplayMode”的未定义引用 opengl1.cpp:(.text+0x16d):对“GLUTINITWindowsSize”的未定义引用 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真正需要什么呢?

在链接器标志中添加“-lstdc++-lGL”。 或者试着像这样编译它:

g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm

(编辑:添加-LGLU,删除-LSTDC++)

< P>确保您使用<代码> G++<代码>来构建C++文件,避免与C++标准库相关的链接错误。


要解析GLUT符号,您可能只需要将
-lglut
添加到最终构建命令中。

您还应该将其与GLUT和GLU链接:

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

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

在编译代码时,必须根据包含的内容提及一些参数。 您已经使用了
-lGL
-lm
等。 要使用
gluOrtho2D
要使用的参数是
-lGLU

对于以
glut
开始的其他函数,使用
-lglut

构建应用程序时,您是否在所有适当的库中链接?我包括了这些头文件:#包括#也包括,您是使用
g++
还是
gcc
构建的?包含头文件只是您需要做的一半。这允许程序编译,但不允许链接。最终创建应用程序(或库)的命令应该链接到您使用的库,否则它将不知道从何处获取所有这些符号。发布你的构建命令,我们可以给你一些指针。使用<代码> G++<代码>来构建C++源代码。您将发现与
std::cout
等相关的错误,然后消失<代码> G++< /COD>实际上是一个包装,围绕着 Gcc,它自动链接到标准C++库。如果使用
gcc
,则必须手动执行。使用
g++
要容易得多。现在我
m在函数中获取/tmp/ccCJBuIl.o:
MyInit():opengl1.cpp:(.text+0x12d):对
gluOrtho2D'/tmp/ccCJBuIl.o:在函数中的未定义引用
main:opengl1.cpp:(.text+0x14a):对
gluit'opengl1.cpp:(.text+0x156)的未定义引用:未定义对
glutInitDisplayMode'opengl1.cpp:(.text+0x16d)的引用:未定义对opengl1.cpp:(.text+0x181)的引用:未定义对
glutInitWindowPosition'opengl1.cpp:(.text+0x19e)的引用:未定义对
glutDisplayFunc'opengl1.cpp:(.text+0x1a3):未定义对'glutMainLoop'的引用:ld返回1