Opengl linux 未定义对基本函数的引用 [英] Opengl linux undefined reference to basic functions
问题描述
我在 Ubuntu 11.04 上编写了一个使用 freeglut 的程序.它工作得很好.然后我得到了另一台计算机并尝试在全新安装的 Ubuntu 11.04 上运行该程序.不起作用.所以我安装了
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
sudo apt-get install freeglut3 freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu
1-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 libglu
1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev mesa-common-dev gcc
并尝试运行导入的程序
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
使用命令
g++ -lGL -lGLU -lglut Driver.cpp -o a
但是链接器或其他任何东西会吐出 200 个表单错误:
However the linker or whatever spits out like 200 errors of the form:
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'
问题的原因是什么?
推荐答案
指定要链接到的对象(包括静态和动态库)的顺序很重要.
The order in which you specify the objects you want to link to (including static and dynamic libraries) can matter.
尝试:
g++ Driver.cpp -lGL -lGLU -lglut -o a
(不确定库的顺序,但看起来没问题.)
(Not sure about the order of the libs, but that looks ok.)
构建命令行时的想法是,如果a
需要来自b
的符号,b
必须出现在之后 a
在命令行中.
The idea when you build your command line is that if a
requires a symbol from b
, b
must appear after a
in the command line.
共享库的 GCC/ld 发生(或不发生)链接顺序问题取决于(很可能在其他方面 - 我不是这里的专家)是否 --as-needed
链接标志是否设置.(例如,参见 Gentoo 的 as-needed 转换中的最后一项指南.)
当 --as-needed
处于活动状态时,链接过程会尽快消除不需要的符号,如果链接顺序不正确",则会导致问题.这样做是为了减少最终可执行文件中存在的不必要依赖项的数量.
如果 --as-needed
未处于活动状态,则不会(或更少)发生这种情况 - 所有符号都保留在这种情况下,并且链接顺序无关紧要(或多或少 - 再次,我不是专家.)
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-needed
link flag is set or not. (See for instance the before-last item in Gentoo's as-needed transition guide.)
The linking process eliminates un-needed symbols ASAP when --as-needed
is 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-needed
is 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 的行为可能看起来不一致,但这只是一种印象.
Since different distributions use different defaults for that flag, the behavior of GCC might seem inconsistent, but that's just an impression.
这篇关于Opengl linux 未定义对基本函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!