OpenGL/glut/stdc++ build errors

江枫思渺然 提交于 2020-01-03 10:54:32

问题


I'm using Ubuntu and tried using synaptic to install everything that had the word "GLUT" in it and also SDL and opengl . But still a simple program fails to compile . It shows this :

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

If I use g++ instead of gcc I get this:

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

So what do I really need to start working with opengl in Ubuntu?


回答1:


Add "-lstdc++ -lGL" to your linker flags. Or try to compile it like this:

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

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




回答2:


You should link it with glut and GLU as well:

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

Files ended with .cpp must be compiled with g++. The rest of the errors are related to the linking process, and they should not happen if you build your application with the command I suggested above. If they do, make sure you have installed libglut and libglu.




回答3:


Make sure you are using g++ to build your C++ files to avoid the linker errors pertaining to the C++ standard library.

To resolve the GLUT symbols you probably just need to add -lglut to your final build command.




回答4:


While compiling the code you must mention some parameters according to your includes. You already used -lGL, -lm etc. To use gluOrtho2D the parameter to be used is -lGLU and for other functions that begin with glut use -lglut



来源:https://stackoverflow.com/questions/5410832/opengl-glut-stdc-build-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!