opengl - 链接器错误: undefined reference to symbol 'glOrtho'

标签 opengl linker-errors

我在 Ubuntu 13.04 ( *mesa-common-dev freeglut3-dev* ) 中安装了 OpenGL 软件包并尝试运行示例程序。

#include "GL/freeglut.h"
#include "GL/gl.h"

/* display function - code from:
     http://fly.cc.fer.hr/~unreal/theredbook/chapter01.html
This is the actual usage of the OpenGL library. 
The following code is the same for any platform */
void renderFunction()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

/* Main method - main entry point of application
the freeglut library does the window creation work for us, 
regardless of the platform. */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");
    glutDisplayFunc(renderFunction);
    glutMainLoop();    
    return 0;
}

但是,我遇到了这个错误,并且不知道该怎么办。

ved@vedvals:~/Desktop/p1$ g++ p1.cpp -lglut
/usr/bin/ld: /tmp/ccgGdeR2.o: undefined reference to symbol 'glOrtho'
/usr/bin/ld: note: 'glOrtho' is defined in DSO /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 so try adding it to the linker command line
/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

我看了
OpenGL hello.c fails to build using CMake
因为错误类似,但我没有使用 CMake

我的代码是错误的还是我需要包含/更改/调整一些设置?

我引用了这个网站进行安装和代码:

Setting up an OpenGL development environment in Ubuntu Linux

最佳答案

您需要链接 OpenGL 库:

g++ p1.cpp -lglut -lGL

关于opengl - 链接器错误: undefined reference to symbol 'glOrtho' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766420/

相关文章:

c++ - Win32 窗口中的 OpenGL 图元根据窗口大小而变形

ios - 突然: Core Plot in iOS: Apple LLVM compiler 4,1 Error

c++ - xcode链接器错误

c++ - 可变参数模板的链接器错误

c++ - Assimp 样本给出错误

opengl - 如何在OpenGL中归一化纹理空间的图像坐标?

qt - Qt 4 中的 OpenGL 更新率

C++ 头文件重新定义错误

ios - clang : error: linker command failed with exit code 1, Xcode 链接器错误

c++ - 获取链接错误 : Extern in C++. 如何访问在文件 A.CPP 中修改的变量的值在另一个文件文件 B.CPP 中