【问题标题】:Compiling and runnin OpenGL (glut) program in ubuntu 10.10 [duplicate]在ubuntu 10.10中编译和运行OpenGL(过剩)程序[重复]
【发布时间】:2011-07-14 10:35:56
【问题描述】:

我写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glx.h>    
#include <GL/gl.h>
#include <GL/glut.h>


void init()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,200.0,0.0,150.0);
 }

 void lineSegment()
 {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);
        glBegin(GL_TRIANGLES);
            glVertex2i(40,120);
            glVertex2i(40,20);
            glVertex2i(80,20);
        glEnd();
    glFlush();
 }
 int main()
 {
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(400,300);
    glutCreateWindow("An Example OpenGL....");
    init();
    glutDisplayFunc(lineSegment);
    glutMainLoop();
    return 0;
 }

这是我的错误。

funfullson@funfullson:~$ gcc gl.cpp 
/tmp/cchfRGT2.o: In function `init()':
gl.cpp:(.text+0x2a): undefined reference to `glClearColor'
gl.cpp:(.text+0x36): undefined reference to `glMatrixMode'
gl.cpp:(.text+0x5a): undefined reference to `gluOrtho2D'
/tmp/cchfRGT2.o: In function `lineSegment()':
gl.cpp:(.text+0x6e): undefined reference to `glClear'
gl.cpp:(.text+0x8d): undefined reference to `glColor3f'
gl.cpp:(.text+0x99): undefined reference to `glBegin'
gl.cpp:(.text+0xad): undefined reference to `glVertex2i'
gl.cpp:(.text+0xc1): undefined reference to `glVertex2i'
gl.cpp:(.text+0xd5): undefined reference to `glVertex2i'
gl.cpp:(.text+0xda): undefined reference to `glEnd'
gl.cpp:(.text+0xdf): undefined reference to `glFlush'
/tmp/cchfRGT2.o: In function `main':
gl.cpp:(.text+0xf6): undefined reference to `glutInitDisplayMode'
gl.cpp:(.text+0x10a): undefined reference to `glutInitWindowPosition'
gl.cpp:(.text+0x11e): undefined reference to `glutInitWindowSize'
gl.cpp:(.text+0x12a): undefined reference to `glutCreateWindow'
gl.cpp:(.text+0x13b): undefined reference to `glutDisplayFunc'
gl.cpp:(.text+0x140): undefined reference to `glutMainLoop'
/tmp/cchfRGT2.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

我不知道我必须做什么。请教我怎么做。

【问题讨论】:

  • 我开始厌倦了,但无论如何:投影矩阵和视口设置属于显示例程,而不是一些晦涩的“init”函数。 -- 您的实际问题的解决方案已经在答案中给出。

标签: opengl


【解决方案1】:

首先,你应该使用g++来编译C++代码,而不是gcc。

然后,您必须在构建程序时链接到 OpenGL 库:

$ g++ gl.cpp -o gl -lGL -lGLU -lglut

上面的命令行将在当前目录中生成一个名为gl 的可执行文件。如果没有-o,生成的可执行文件称为a.out(在我的平台上),这可能不是您想要的。

(请注意,根据下面密码的注释,源文件必须位于库选项之前,以便g++ 编译它们。)

【讨论】:

  • 他应该mv gl.cpp gl.c,因为没有使用任何 C++。
  • @mathepic,你可能是对的,但我不能假设:也许cpp 扩展名实际上是有意义的,并且代码将在一周内充满类;)
  • @FrédéricHamidi :看起来您必须将文件放在任何 -l 选项的前面,就像@warl0ck 在他的回答中所说的那样!
  • @cipher,真的吗?什么让你有那个想法?对于目标文件可能是这样(它们应该在库之前被提及,以便可以提取所有必需的符号)但对于源文件不是这样,至少根据我的经验。
  • @cipher,很有趣,我现在没有方便的g++ 来仔细检查这一点,但我相信你。我会相应地更新我的答案。感谢您的提醒:)
【解决方案2】:

您必须将文件放在任何 -l 选项的前面,这里是一个示例:

g++ X.cpp -lX11 -lGL -lGLU -lglut -g -Wall -O2 -o X

还要确保以下软件包已经存在:

sudo apt-get install mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

并确保 glx 模块已加载到 Xorg 中

Section "Module"
    Load "glx"
EndSection

祝 OpenGL 编程好运!

【讨论】:

  • 在添加-lX11 编译命令后,它在 ubuntu 16.04lts 上工作正常
【解决方案3】:

用于编译

g++ filename.cpp -lGL -lGLU -lglut

执行

./a.out

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    相关资源
    最近更新 更多