eclipse Ubuntu 13.10 C++ OpenGL GLUT - 链接问题 - 对“glClearColor”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21244712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:29:01  来源:igfitidea点击:

Ubuntu 13.10 C++ OpenGL GLUT - linking issues - undefined reference to `glClearColor'

c++eclipseopenglubuntufreeglut

提问by NeverEndingQueue

I am running Ubuntu 13.10 and trying to compile that portion of sample OpenGL code:

我正在运行 Ubuntu 13.10 并尝试编译示例 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;
}

I've used Eclipse to build the project, however it fails on linking level:

我已经使用 Eclipse 来构建项目,但是它在链接级别上失败了:

Building target: opengl_test
Invoking: GCC C++ Linker
g++ -L/usr/lib -L/usr/lib/fglrx -L/usr/lib/x86_64-linux-gnu -L/usr/lib/i386-linux-gnu/mesa -L/usr/lib/x86_64-linux-gnu/mesa -o "opengl_test"  ./src/opengl_test.o   
./src/opengl_test.o: In function `renderFunction()':
/var/www/opengl_test/Debug/../src/opengl_test.cpp:10: undefined reference to `glClearColor'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:11: undefined reference to `glClear'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:12: undefined reference to `glColor3f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:13: undefined reference to `glOrtho'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:14: undefined reference to `glBegin'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:15: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:16: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:17: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:18: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:19: undefined reference to `glEnd'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:20: undefined reference to `glFlush'
./src/opengl_test.o: In function `main':
/var/www/opengl_test/Debug/../src/opengl_test.cpp:28: undefined reference to `glutInit'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:29: undefined reference to `glutInitDisplayMode'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:30: undefined reference to `glutInitWindowSize'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:31: undefined reference to `glutInitWindowPosition'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:32: undefined reference to `glutCreateWindow'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:33: undefined reference to `glutDisplayFunc'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:34: undefined reference to `glutMainLoop'
collect2: error: ld returned 1 exit status
make: *** [opengl_test] Error 1

... so I've tried G++:

...所以我试过 G++:

g++ -lGL -lglut opengl_test.cpp  -o test

However it gives pretty much the same output as Eclipse one.

然而,它提供了与 Eclipse 几乎相同的输出。

I thought that maybe I need symlink necessary libs manually to /usr/lib. I've searched for libs:

我想也许我需要手动将必要的库符号链接到 /usr/lib。我已经搜索了库:

$ sudo apt-file search libGL.so
fglrx: /usr/lib/fglrx/libGL.so
fglrx: /usr/lib/fglrx/libGL.so.1
fglrx: /usr/lib/fglrx/libGL.so.1.2
fglrx: /usr/lib32/fglrx/libGL.so.1.2
fglrx-updates: /usr/lib/fglrx/libGL.so
fglrx-updates: /usr/lib/fglrx/libGL.so.1
fglrx-updates: /usr/lib/fglrx/libGL.so.1.2
fglrx-updates: /usr/lib32/fglrx/libGL.so.1.2
libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/libGL.so
libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/mesa/libGL.so
libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1
libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
libgl1-mesa-glx-dbg: /usr/lib/debug/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
nvidia-173: /usr/lib/nvidia-173/libGL.so
nvidia-173: /usr/lib/nvidia-173/libGL.so.1
nvidia-173: /usr/lib/nvidia-173/libGL.so.173.14.37
nvidia-173: /usr/lib32/nvidia-173/libGL.so
nvidia-173: /usr/lib32/nvidia-173/libGL.so.1
nvidia-173: /usr/lib32/nvidia-173/libGL.so.173.14.37
nvidia-304: /usr/lib/nvidia-304/libGL.so
nvidia-304: /usr/lib/nvidia-304/libGL.so.1
nvidia-304: /usr/lib/nvidia-304/libGL.so.304.88
nvidia-304: /usr/lib32/nvidia-304/libGL.so
nvidia-304: /usr/lib32/nvidia-304/libGL.so.1
nvidia-304: /usr/lib32/nvidia-304/libGL.so.304.88
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.1
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.304.108
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.1
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.304.108
nvidia-319: /usr/lib/nvidia-319/libGL.so
nvidia-319: /usr/lib/nvidia-319/libGL.so.1
nvidia-319: /usr/lib/nvidia-319/libGL.so.319.32
nvidia-319: /usr/lib32/nvidia-319/libGL.so
nvidia-319: /usr/lib32/nvidia-319/libGL.so.1
nvidia-319: /usr/lib32/nvidia-319/libGL.so.319.32
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.1
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.319.60
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.1
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.319.60
primus-libs: /usr/lib/x86_64-linux-gnu/primus/libGL.so.1

and

$ sudo apt-file search libglut.so
freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3
freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0
freeglut3-dev: /usr/lib/x86_64-linux-gnu/libglut.so

Made symlinks:

制作符号链接:

sudo ln -s /usr/lib/fglrx/libGL.so /usr/lib/libGL.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/libglut.so

However it still did not help. The same linking issue appeared.

然而,它仍然没有帮助。出现了相同的链接问题。

  1. How do I debug that issue?
  2. How do I know which lib to choose and link them to /usr/lib? I chose fglrx, because my GPU drivers are running fglrx.
  3. Do I need each time to link library manually from /usr/lib..../somewhere to /usr/lib?
  1. 我如何调试该问题?
  2. 我怎么知道选择哪个库并将它们链接到 /usr/lib?我选择了 fglrx,因为我的 GPU 驱动程序正在运行 fglrx。
  3. 我是否需要每次都手动将库从 /usr/lib..../somewhere 链接到 /usr/lib?

回答by vbo

Try putting your libraries after the source file:

尝试将您的库放在源文件之后:

g++ opengl_test.cpp -lGL -lglut

To get Eclipse working you need to change so called "command line pattern". I am not Eclipse user actually, but try to check settings here: C/C++ Build -> Settings -> Cross G++ Linker, or some other Linker-related settings.

要让 Eclipse 工作,您需要更改所谓的“命令行模式”。我实际上不是 Eclipse 用户,但尝试在此处检查设置:C/C++ Build -> Settings -> Cross G++ Linker或其他一些与链接器相关的设置。

Clarification: current lddefaults to link with --as-neededflag. In this mode before linking any library ldchecks that some symbols from this library needed somewhere. ldperform this checks in order, so if you link something before your source file this library gets excluded. This behaviour can be disabled using -Wl,--no-as-neededgccflag.

说明:当前ld默认与--as-needed标志链接。在这种模式下,在链接任何库之前ld检查该库中的某些符号是否需要某处。ld按顺序执行此检查,因此如果您在源文件之前链接某些内容,则此库将被排除。可以使用-Wl,--no-as-neededgcc标志禁用此行为。

From man ld:

来自man ld

--as-needed
--no-as-needed
    This option affects ELF DT_NEEDED tags for dynamic  libraries  men-
    tioned on the command line after the --as-needed option.  Normally,
    the linker will add a DT_NEEDED tag for each dynamic     library  men-
    tioned  on  the  command line, regardless of whether the library is
    actually needed.  --as-needed causes     DT_NEEDED  tags  to  only  be
    emitted for libraries that satisfy some symbol reference from regu-
    lar objects which is undefined at the point that  the  library  was
    linked.  --no-as-needed restores the default behaviour.