尝试构建简单的OpenGL程序时链接错误

时间:2014-05-19 03:15:09

标签: opengl ubuntu netbeans linker

这是OpenGL代码:

#include <GL/glut.h>
void display()
{
 glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc,char **argv)
{
   glutInit(&argc,argv);
   glutCreateWindow("Hello,world!");
   glutDisplayFunc(display);
   glutMainLoop();
}

错误消息是:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cppapplication_1
make[2]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -lglut -lGLU -lGL -lGLEW    -o dist/Debug/GNU-Linux-x86/cppapplication_1 build/Debug/GNU-Linux-x86/main.o -L/usr/lib/x86_64-linux-gnu -Wl,-rpath,/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGLEW.so /usr/lib/x86_64-linux-gnu/libGLEWmx.so
/usr/bin/ld: build/Debug/GNU-Linux-x86/main.o: undefined reference to symbol 'glClear'
/usr/lib/x86_64-linux-gnu/libGL.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cppapplication_1] Error 1
make[2]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 124ms)

起初我以为这是因为安装的OpenGL版本太低,但是glClear可以从OpenGL 1.0获得并且存在于所有版本中(see here)。这是我系统上OpenGL的版本信息。 enter image description here

我构建了freeglut 2.8.1和glew 1.10.0并将它们安装在我的系统中: enter image description here

我包含了库的路径并在环境中指定了所需的库: enter image description here enter image description here

此外,我已阅读存储库中的相关主题:enter link description hereenter link description here,但它们没有帮助。

我已经疲惫不堪,真的找不到我所缺少的东西来构建这么简单的OpenGL代码。你能告诉我如何解决这个问题吗?谢谢。

我正在使用的环境是: Ubuntu 14.04(64位)+ NetBeans 8.0

提示:当我评论glClear时,程序可以成功构建。

编辑:对于那些在Windows 7上工作并遇到类似链接问题(未找到OpenGL函数)的人,Vishwanath gowda在this帖子中的答案可能会有所帮助。

Edit2:我们知道Windows支持OpenGL很差,如果你同时使用英特尔的入门级集成显卡,英特尔的驱动程序不提供OpenGL的额外支持,你将不得不制作一个新的Mesa的OpenGL库根据指南here。这可以做到,因为OpenGL独立于硬件,因此可以仅通过软件实现(A book声明)。如果您使用的是64位Win7,请小心使用machine = x86_64。您可以通过观察dumpbin / headers的输出来检查youropengldll.dll | more。您还可以使用“OpenGL Extension Viewer”软件检查Windows系统上OpenGL的功能是否得到增强。

2 个答案:

答案 0 :(得分:8)

我可以使用以下命令在64位Ubuntu 14.04系统上编译您的示例程序:

g++ example.c -lGL -lGLU -lGLEW -lglut -o example

链接选项的顺序很重要:必须在依赖于它们的目标文件之后指定库。来自GCC documentation for the -l option

  

在您编写此选项的命令中,它会有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,‘foo.o -lz bar.o’在文件‘z’之后但foo.o之前搜索库bar.o。如果bar.o引用‘z’中的函数,则可能无法加载这些函数。

(奇怪的是,即使我先将-l选项放在上,程序也会在我的Debian系统上编译没有错误。但不是在Ubuntu上。)

答案 1 :(得分:2)

好的,我找到了关键:我应该明确指定一个包含所需OpenGL函数的库文件:libGL.so,如下图所示:

enter image description here

再添加几行,这是这个简单的OpenGL程序的输出:

enter image description here

我认为这应该是特定于NetBeans的问题,可能不会出现在像eclipse这样的其他IDE上。

编辑:虽然上述方法有效,但关键是在NetBeans中设置链接选项。我们需要做的就是在项目属性中键入“-lGL -lGLU -lGLEW -lglut” - &gt;链接器 - &gt;其他选项,如下图所示:

enter image description here

感谢Wyzard指出这一点。