尝试构建简单的OpenGL程序时出现链接错误 [英] Link error when trying to build a simple OpenGL program

查看:120
本文介绍了尝试构建简单的OpenGL程序时出现链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是OpenGL代码:

This is the OpenGL code:

#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中可用,并且在所有版本中都存在(

At first I had thought this is because the OpenGL version installed is too low, but glClear is available from OpenGL 1.0 and exists in all versions (see here). This is version information of the OpenGL on my system.

我构建了freeglut 2.8.1并运行了1.10.0,并将它们安装在我的系统中:

I built the freeglut 2.8.1 and glew 1.10.0 and have them installed in my system:

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

I included the path to the library and specified needed library in the environment:

我还阅读了存储库中的相关主题:输入链接描述输入链接描述,但它们无济于事.

Also, I have read related threads in repository: enter link description here, enter link description here, but they don't help.

我已经筋疲力尽了,真的找不到构建如此简单的OpenGL代码所缺少的东西.您能告诉我如何解决此问题吗?谢谢.

I'm worn out and really can not find what I am missing to build such a simple OpenGL code. Could you please tell me how to troubleshoot this problem? Thank you.

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

The environment I am using are: Ubuntu 14.04 (64 bit)+ NetBeans 8.0

提示:当我注释掉glClear时,程序可以成功构建.

A hint: when I commented the glClear out, the program can be built successfully.

对于那些在Windows 7上工作并且遇到类似的链接问题(未找到OpenGL函数)的人,Vishwanath gowda在

For those who working on windows 7 and encountering similar linking problem (OpenGL function not found), Vishwanath gowda's answer in this thread may help.

Edit2:我们知道Windows对OpenGL的支持很差,如果您同时使用Intel的驱动程序不提供OpenGL附加支持的Intel入门级集成显卡,则必须制作一个新的Mesa的OpenGL libray根据指南此处.之所以可以这样做是因为OpenGL与硬件无关,因此可以仅通过软件来实现( book 声明所以).如果您正在使用64位Win7,请小心使用machine = x86_64.您可以通过观察dumpbin/headers youropengldll.dll | more的输出来进行检查.使用软件"OpenGL Extension Viewer"之后,还可以检查Windows系统上OpenGL的功能是否得到增强.

We know that Windows supports OpenGL poorly, if you are at the same time using Intel's entrance-level integrated graphics card for which intel's driver provides no additional support of OpenGL, you'll have to make a new Mesa's OpenGL libray according to the guide here. This can be done because OpenGL is independent of hardware so can be implemented solely by software (A book claims so). Be careful to use machine=x86_64 if you are working on 64bit Win7. You can check that by observing the output of dumpbin /headers youropengldll.dll|more. You can also check that the functionality of OpenGL on your windows system is enhanced after that using a software "OpenGL Extension Viewer".

推荐答案

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

I'm able to compile your example program on a 64-bit Ubuntu 14.04 system using this command:

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

链接选项的顺序很重要:必须在依赖它们的对象文件后 中指定库.从 -l的GCC文档中选项:

The order of the link options is important: libraries have to be specified after the object files that depend on them. From the GCC documentation for the -l option:

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

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系统上编译而没有错误.)

(Strangely, the program compiles without errors on my Debian systems even if I put the -l options first. But not on Ubuntu.)

这篇关于尝试构建简单的OpenGL程序时出现链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆