Opengl linux 未定义对基本函数的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10000925/
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-08-06 05:35:56  来源:igfitidea点击:

Opengl linux undefined reference to basic functions

c++linuxopengl

提问by comp sci balla

I wrote a program on Ubuntu 11.04 that uses freeglut. It worked fine. Then I got another computer and tried to run the program on a fresh install of Ubuntu 11.04. Doesn't work. So I installed

我在 Ubuntu 11.04 上编写了一个使用 freeglut 的程序。它工作得很好。然后我得到了另一台计算机并尝试在全新安装的 Ubuntu 11.04 上运行该程序。不起作用。所以我安装了

sudo apt-get install freeglut3 freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev mesa-common-dev gcc

sudo apt-get install freeglut3 freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev mesa-common-dev gcc

and tried to run the program, which imports

并尝试运行该程序,该程序导入



    #include <GL/freeglut.h>
    #include <GL/gl.h>
    #include <GL/glu.h>

using the command

使用命令

g++ -lGL -lGLU -lglut Driver.cpp -o a

g++ -lGL -lGLU -lglut Driver.cpp -o a

However the linker or whatever spits out like 200 errors of the form:

然而,链接器或其他类似形式的 200 个错误会吐出:



    Driver.cpp:(.text+0x3c6b): undefined reference to `glutSolidSphere'
    Driver.cpp:(.text+0x3c75): undefined reference to `glEnable'
    Driver.cpp:(.text+0x3c9a): undefined reference to `glColor4f'
    Driver.cpp:(.text+0x3cb5): undefined reference to `glRotatef'
    Driver.cpp:(.text+0x3d02): undefined reference to `glutSolidSphere'
    Driver.cpp:(.text+0x3d07): undefined reference to `glutSwapBuffers'

What is the cause of the problem?

问题的原因是什么?

采纳答案by Mat

The order in which you specify the objects you want to link to (including static and dynamic libraries) can matter.

指定要链接到的对象(包括静态和动态库)的顺序可能很重要。

Try with:

尝试:

g++ Driver.cpp -lGL -lGLU -lglut  -o a

(Not sure about the order of the libs, but that looks ok.)

(不确定库的顺序,但看起来没问题。)

The idea when you build your command line is that if arequires a symbol from b, bmust appear afterain the command line.

当你建立你的命令行的想法是,如果a需要从一个符号bb必须出现后,a在命令行。

The link order problem happens (or not) with GCC/ld for shared libraries depending on (most likely among other things - I'm no expert here) whether the --as-neededlink flag is set or not. (See for instance the before-last item in Gentoo's as-neededtransition guide.)
The linking process eliminates un-needed symbols ASAP when --as-neededis active, which causes problems if the link order is not "correct". This is done to reduce the number of un-necessary dependencies present in final executables.
This doesn't happen (or less so) if --as-neededis not active - all symbols are kept in that case, and link order doesn't matter as much (more or less - again, I'm no expert.)

链接顺序问题发生(或不发生)与共享库的 GCC/ld 取决于(很可能在其他方面 - 我不是这里的专家)是否--as-needed设置了链接标志。(例如参见 Gentoo按需转换指南中的前最后一项。)
链接过程在--as-needed激活时会尽快消除不需要的符号,如果链接顺序不“正确”,则会导致问题。这样做是为了减少最终可执行文件中存在的不必要依赖项的数量。
如果--as-needed不活动,则不会发生(或更少)- 所有符号都保留在这种情况下,并且链接顺序无关紧要(或多或少 - 再次,我不是专家。)

Since different distributions use different defaults for that flag, the behavior of GCC might seem inconsistent, but that's just an impression.

由于不同的发行版对该标志使用不同的默认值,GCC 的行为可能看起来不一致,但这只是一种印象。