【问题标题】:Error with GLUT compile in ubuntu [duplicate]在 ubuntu 中编译 GLUT 时出错 [重复]
【发布时间】:2012-01-11 05:41:45
【问题描述】:

我尝试编译一些“hello world” glut 应用程序:

#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();
}

编译命令为:

gcc -lGL -lGLU hw_opengl.cpp -o hw_opengl

我遇到以下错误:

/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 install freeglut3 freeglut3-dev

/usr/lib 中有: libglut.a
libglut.so
libglut.so.3
libglut.so.3.9.0

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

我做错了什么?

【问题讨论】:

    标签: c++ opengl glut glu


    【解决方案1】:

    你错过了-lglut

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

    【讨论】:

    • 我试试这个编译命令:gcc -lGL -lglut -lGLU hw_opengl.cpp -o hw_opengl。有同样的错误。
    • 嗯,这很奇怪。我按照您发布的命令运行了该命令,并且可以正常编译。我运行了你的程序,我看到了一个红色方块,如代码所示。这表明您的编译器不知道该库 - 尝试运行 ldconfig(以 root 身份),或者可能重新启动。你运行的是什么操作系统?
    • 我用的是 kubuntu 11.10 $ gcc -lgl -lglut -lglu hw_opengl.cpp -o hw_opengl /usr/bin/ld: 找不到 -lgl /usr/bin/ld: 找不到 -lglu跨度>
    • 你是否也安装了libgl-dev(也可能叫libgl)?
    • 我已经安装了 xlibmesa-gl-dev 和 xlibmesa-glu-dev
    【解决方案2】:

    这是因为 xlibmesa-gl-dev 和 xlibmesa-glu-dev 没有对文件 libGL.so 和 libGLU.so 进行软链接,所以 ld 找不到它们与您的代码链接。

    【讨论】:

    • 因为 ld 会为选项 -lGL 和 -lGLU 寻找 libGL.so 和 libGLU.so,但是 xlibmesaxxx 不会将这两个软链接指向它们自己的库。
    【解决方案3】:

    GCC 链接器可能会按照它们在命令行上的顺序扫描库,这意味着它可能会先扫描这些库并且发现没有人使用它们,因此您会收到错误消息。可以肯定的是,将库放在命令行的最后:

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

    【讨论】:

    【解决方案4】:

    检查你是否安装了binutils-gold(Gold linker),如果没有,安装它,如果你已经安装了,然后删除它并在终端尝试,

    g++ sourcefile.cpp -lglut
    

    【讨论】:

      【解决方案5】:
      g++ -lglut -lGLU -lGL helloWorld.cpp -o helloWorld
      

      g++,不是gcc,用于 cpp

      【讨论】:

      • 如果您想使用 gcc,请使用 -std=c++11 或添加标志 -lstdc++
      【解决方案6】:
      g++ filename.cpp -lGL -lglut -o exfilename
      

      然后

      ./exfilename
      

      【讨论】:

        猜你喜欢
        • 2013-01-17
        • 2021-03-23
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 2015-01-05
        • 2014-12-10
        • 2014-12-05
        • 1970-01-01
        相关资源
        最近更新 更多