微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这是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的版本信息.

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

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

此外,我已阅读存储库中的相关线程:enter link description here,enter link description here,但它们没有帮助.

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

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

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

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

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

我可以使用以下命令在64位Ubuntu 14.04系统上编译您的示例程序:
g++ example.c -lGL -lglu -lGLEW -lglut -o example

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

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus,‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’,those functions may not be loaded.

(奇怪的是,即使我把-l选项放在第一位,程序也会在我的Debian系统上编译而没有错误.但不是在Ubuntu上.)

原文地址:https://www.jb51.cc/ubuntu/347221.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐