关于c ++:在ubuntu中使用GLUT编译时出错

Error with GLUT compile in ubuntu

本问题已经有最佳答案,请猛点这里访问。

我尝试编译一些"hello world"GLUT应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdlib.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glut.h>

GLint Width = 512, Height = 512;

const int CubeSize = 200;

void Display(void)
{
    int left, right, top, bottom;

    left  = (Width - CubeSize) / 2;
    right = left + CubeSize;
    bottom = (Height - CubeSize) / 2;
    top = bottom + CubeSize;

    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3ub(255,0,0);
    glBegin(GL_QUADS);
      glVertex2f(left,bottom);
      glVertex2f(left,top);
      glVertex2f(right,top);
      glVertex2f(right,bottom);
    glEnd();

    glFinish();
}

void Reshape(GLint w, GLint h)
{
    Width = w;
    Height = h;
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, w, 0, h, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Keyboard(unsigned char key, int x, int y)
{
#define ESCAPE '\033'

    if( key == ESCAPE )
        exit(0);
}

main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(Width, Height);
    glutCreateWindow("Red square example");

    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Keyboard);

    glutMainLoop();
}

编译命令是:

1
gcc -lGL -lGLU hw_opengl.cpp -o hw_opengl

我有以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/tmp/ccbnBFHC.o: In function `Display()':
hw_opengl.cpp:(.text+0x6c): undefined reference to `glClearColor'

hw_opengl.cpp:(.text+0x78): undefined reference to `glClear'
hw_opengl.cpp:(.text+0x94): undefined reference to `glColor3ub'

hw_opengl.cpp:(.text+0xa0): undefined reference to `glBegin'
hw_opengl.cpp:(.text+0xb4): undefined reference to `glVertex2f'

hw_opengl.cpp:(.text+0xc8): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xdc): undefined reference to `glVertex2f'

hw_opengl.cpp:(.text+0xf0): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xf5): undefined reference to `glEnd'

hw_opengl.cpp:(.text+0xfa): undefined reference to `glFinish'
/tmp/ccbnBFHC.o: In function `Reshape(int, int)'
:
hw_opengl.cpp:(.text+0x134): undefined reference to `glViewport'
hw_opengl.cpp:(.text+0x140): undefined reference to `glMatrixMode'

hw_opengl.cpp:(.text+0x145): undefined reference to `glLoadIdentity'
hw_opengl.cpp:(.text+0x173): undefined reference to `glOrtho'

hw_opengl.cpp:(.text+0x17f): undefined reference to `glMatrixMode'
hw_opengl.cpp:(.text+0x184): undefined reference to `glLoadIdentity'

/tmp/ccbnBFHC.o: In function `main':
hw_opengl.cpp:(.text+0x1c1): undefined reference to `glutInit'

hw_opengl.cpp:(.text+0x1cd): undefined reference to `glutInitDisplayMode'
hw_opengl.cpp:(.text+0x1e4): undefined reference to `glutInitWindowSize'

hw_opengl.cpp:(.text+0x1f0): undefined reference to `glutCreateWindow'
hw_opengl.cpp:(.text+0x1fc): undefined reference to `glutDisplayFunc'

hw_opengl.cpp:(.text+0x208): undefined reference to `glutReshapeFunc'
hw_opengl.cpp:(.text+0x214): undefined reference to `glutKeyboardFunc'

hw_opengl.cpp:(.text+0x219): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status

我安装了GLUT:sudo apt get安装freegult3 freegult3 dev

在/usr/lib中有:利布格特因此,L.bgulut. S. 3libglut.so.3.9.0版本

1
2
3
4
5
6
7
8
9
locate glu.h
/home/goran/QtSDK/QtSources/4.8.0/src/3rdparty/webkit/Source/ThirdParty/glu/internal_glu.h
/usr/include/GL/glu.h

/usr/include/GL/gl.h

locate gl.h
...
/usr/include/GL/gl.h

我怎么做不正确?


gcc链接器可以按照它们在命令行上的顺序扫描库,这意味着它可以首先扫描库,并且没有人使用它们,因此您会得到错误。要确保,请将库放在命令行的最后一个位置:

1
gcc hw_opengl.cpp -o hw_opengl -lGL -lGLU -lglut


1
g++ filename.cpp -lGL -lglut -o exfilename

然后

1
./exfilename


这是因为xlibmesa-gl-dev和xlibmesa-glu-dev没有建立到libgl.so和libglu.so文件的软链接,所以ld找不到它们与您的代码链接。


你不见了。

正确的编译命令是gcc -lGL -lglut -lGLU hw_opengl.cpp -o hw_opengl


1
g++ -lglut -lGLU -lGL helloWorld.cpp -o helloWorld

用于CPP的g++,而不是gcc


检查是否安装了binutils-gold(金链接器),如果没有,请安装,如果已经安装,请将其卸下,然后在终端上尝试。

1
g++ sourcefile.cpp -lglut