链接错误:未定义的对符号“glOrtho”的引用

时间:2022-11-25 05:37:55

I installed OpenGL packages in Ubuntu 13.04 ( *mesa-common-dev freeglut3-dev* ) and tried to run a sample program.

我在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;
}

However, I encountered this error and don't know what to make of it.

然而,我遇到了这个错误,不知道该怎么做。

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

I had a look at
OpenGL hello.c fails to build using CMake
as the error is similar but I am not using CMake

我看了OpenGL hello。c不能使用CMake,因为错误是相似的,但是我没有使用CMake。

IS my code wrong or do I need to include/change/tweak some settings?

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

I referred this site for installation and code :

我参考了这个网站的安装和代码:

Setting up an OpenGL development environment in Ubuntu Linux

在Ubuntu Linux中建立OpenGL开发环境。

2 个解决方案

#1


21  

You need to link the OpenGL library:

你需要连结OpenGL图书馆:

g++ p1.cpp -lglut -lGL

#2


5  

You have not linked the OpenGL lib, where glOrtho() is defined. To make it work, compile/link with g++ p1.cpp -lglut -lGL. Mind the order of linking libs, as it is important in ld (linker used by g++). The GLUT library depends on OpenGL, and so -lGL HAS TO go after -glut. This is because ld only makes one cycle through the libraries, and thus if you linked -lGL -lglut, references from lglut to lGL will not be defined, thus making a linking error. Sorry for such a long answer, but I hope you'll learn something from it.

您没有链接OpenGL lib,其中glOrtho()是定义的。要使它工作,编译/链接到g++ p1。cpp -lglut -lGL。注意链接libs的顺序,因为它在ld中很重要(链接器使用的是g++)。供过于求的图书馆依赖于OpenGL,所以-lGL不得不去过剩。这是因为ld只在库中生成一个循环,因此,如果链接-lGL - l过剩,那么从l过剩到lGL的引用将不被定义,从而导致链接错误。很抱歉这么长时间的回答,但我希望你能从中学到一些东西。

#1


21  

You need to link the OpenGL library:

你需要连结OpenGL图书馆:

g++ p1.cpp -lglut -lGL

#2


5  

You have not linked the OpenGL lib, where glOrtho() is defined. To make it work, compile/link with g++ p1.cpp -lglut -lGL. Mind the order of linking libs, as it is important in ld (linker used by g++). The GLUT library depends on OpenGL, and so -lGL HAS TO go after -glut. This is because ld only makes one cycle through the libraries, and thus if you linked -lGL -lglut, references from lglut to lGL will not be defined, thus making a linking error. Sorry for such a long answer, but I hope you'll learn something from it.

您没有链接OpenGL lib,其中glOrtho()是定义的。要使它工作,编译/链接到g++ p1。cpp -lglut -lGL。注意链接libs的顺序,因为它在ld中很重要(链接器使用的是g++)。供过于求的图书馆依赖于OpenGL,所以-lGL不得不去过剩。这是因为ld只在库中生成一个循环,因此,如果链接-lGL - l过剩,那么从l过剩到lGL的引用将不被定义,从而导致链接错误。很抱歉这么长时间的回答,但我希望你能从中学到一些东西。