我正在使用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
如果我使用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
那么我真的需要在Ubuntu中开始使用opengl吗?
答案 0 :(得分:7)
将“-lstdc ++ -lGL”添加到链接器标志。 或者尝试像这样编译它:
g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm
(编辑:添加-lGLU,删除-lstdc ++)
答案 1 :(得分:3)
你应该将它与过剩和GLU联系起来:
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU
以 .cpp 结尾的文件必须使用 g ++ 进行编译。其余的错误与链接过程有关,如果使用我上面建议的命令构建应用程序,则不应该发生这些错误。如果有,请确保已安装 libglut 和 libglu 。
答案 2 :(得分:2)
确保使用g++
构建C ++文件,以避免与C ++标准库相关的链接器错误。
要解析GLUT符号,您可能只需要将-lglut
添加到最终构建命令中。
答案 3 :(得分:0)
编译代码时,必须根据包含内容提及一些参数。
您已使用-lGL
,-lm
等。
要使用gluOrtho2D
,要使用的参数为-lGLU
以及以glut
开头的其他功能使用-lglut