1.cpp
#include <GL/glut.h> //glut.h中包含头文件gl.h和glu.h void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void SetupRC(void) { glClearColor(0.0f,1.0f,1.0f,1.0f); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); glutCreateWindow("Simple"); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }
$ g++ 1.cpp -o 1 -lglut
结果得到错误如下:
/usr/bin/ld: /tmp/ccZJJ3dA.o: undefined reference to symbol 'glClear'
/usr/bin/ld: note: 'glClear' is defined in DSO /usr/lib/libGL.so.1 so try adding it to the linker command line
/usr/lib/libGL.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
没有找到libGL.so.1, 所以定位libGL:
$ locate libGL
得到:
/usr/lib/i386-linux-gnu/libGL.so
/usr/lib/i386-linux-gnu/libGLEW.so.1.6
/usr/lib/i386-linux-gnu/libGLEW.so.1.6.0
/usr/lib/i386-linux-gnu/libGLEWmx.so.1.6
/usr/lib/i386-linux-gnu/libGLEWmx.so.1.6.0
/usr/lib/i386-linux-gnu/libGLU.a
/usr/lib/i386-linux-gnu/libGLU.so
/usr/lib/i386-linux-gnu/libGLU.so.1
/usr/lib/i386-linux-gnu/libGLU.so.1.3.08004
/usr/lib/i386-linux-gnu/mesa/libGL.so
/usr/lib/i386-linux-gnu/mesa/libGL.so.1
/usr/lib/i386-linux-gnu/mesa/libGL.so.1.2.0
再尝试加上库lGL:
$ g++ 1.cpp -o 1 -lglut -lGL
下面,在这个窗口内画一个长方形:
#include <GL/glut.h> //glut.h中包含头文件gl.h和glu.h void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glRectf(-25.0f,25.0f,25.0f,-25.0f); glFlush(); } void SetupRC(void) { glClearColor(0.0f,0.0f,1.0f,1.0f); } void ChangeSize(GLsizei w, GLsizei h) { GLfloat aspectRatio; if(h == 0) h = 1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); aspectRatio = (GLfloat)w / (GLfloat)h; if(w <= h) glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho(-100 / aspectRatio, 100.0 / aspectRatio, -100.0, 100.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); glutCreateWindow("Simple"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); //Control viewport to draw rectangle SetupRC(); glutMainLoop(); return 0; }重新用g++编译: