这是 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 中的回答线程可能会有所帮助。
Edit2:我们知道Windows对OpenGL的支持很差,如果你同时使用Intel的入门级集成显卡,而intel的驱动不提供额外的OpenGL支持,你就得重新制作一个Mesa的OpenGL库根据指南here .这是可以做到的,因为 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 filefoo.o
but beforebar.o
. Ifbar.o
refers to functions in‘z’
, those functions may not be loaded.
(奇怪的是,即使我将 -l
选项放在 前面,程序在我的 Debian 系统上编译也没有错误。但在 Ubuntu 上则不然。)
关于opengl - 尝试构建简单的 OpenGL 程序时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729213/