C ++ OpenGL未定义引用 [英] c++ OpenGL undefined references
问题描述
我正在尝试通过遵循教程来使OpenGL在使用c ++的Linux机器上运行.
我正在使用Ubuntu 12.04 LTS.我有两个由sudo apt-get install package-name1 package-name2
I am using Ubuntu 12.04 LTS. I have both packages freeglut3-dev and mesa-common-dev
which I installed by sudo apt-get install package-name1 package-name2
只需确保我进入了推荐的目录/usr/include/GL
,并用ls
列出了文件:
Just to be sure I went to the recommended directory /usr/include/GL
and listed the files with ls
:
freeglut_ext.h freeglut.h freeglut_std.h glext.h gl.h gl_mangle.h glu.h glu_mangle.h glut.h glxext.h glx.h glx_mangle.h internal
我注意到我确实有gl.h
和glut.h
,所以我按照教程的建议测试了示例程序.我下载了zip文件,将文件解压缩到同一目录,然后运行make
实用程序.这是运行make
I noted that I do have gl.h
and glut.h
so I tested the sample program as recommended by the tutorial. I downloaded the zip file, unzipped the files into the same directory and then ran the make
utility. Here is some output after running make
g++ -Wall -o cube main.cpp imageloader.cpp -lglut
imageloader.cpp: In function ‘Image* loadBMP(const char*)’:
imageloader.cpp:141:2: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
/tmp/ccWT2wvp.o:main.cpp:function loadTexture(Image*): error: undefined reference to 'glGenTextures'
/tmp/ccWT2wvp.o:main.cpp:function loadTexture(Image*): error: undefined reference to 'glBindTexture'
/tmp/ccWT2wvp.o:main.cpp:function loadTexture(Image*): error: undefined reference to 'glTexImage2D'
我还有大约20个其他未定义的参考错误.因此,必须是我没有正确安装某些东西,或者缺少文件.我确实注意到我通过运行glxinfo
检查了硬件加速信息,并且确实具有硬件加速.
I got about 20 other undefined reference errors. So, it must be that I do not have something installed correctly, or am missing files. I do note that I checked the hardware acceleration info by running glxinfo
and I do have hardware acceleration.
- 如何使用c ++使OpenGL正常工作
编辑:如果我同时使用了两个答案,则证明它是可行的:具体来说,我做了以下事情:
EDIT If I use a combination of both answers proved it works: Specifically I did the following:
someonespc:~/c++$ ls
imageloader.cpp imageloader.h main.cpp Makefile Makefile~ vtr.bmp
d2b2@d2b2-Aspire-7745G:~/c++$ g++ -Wall -lGL -o cube main.cpp imageloader.cpp -lglut -lGLU
imageloader.cpp: In function ‘Image* loadBMP(const char*)’:
imageloader.cpp:141:2: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
someonespc:~/c++$ ls
cube imageloader.cpp imageloader.h main.cpp Makefile Makefile~ vtr.bmp
然后我运行./cube' and it worked! Please explain why putting
-lGL and
-`lGLU工作了
Then I ran ./cube' and it worked! Please explain why putting
-lGLand
-`lGLU worked
推荐答案
将-lGL
添加到gcc命令行选项.它告诉链接器这些功能可能在libGL.so
(这是libGL.so.1
的链接)中找到.
Add -lGL
to the gcc command line options. It tells the linker that the functions might be found in libGL.so
(which is a link to libGL.so.1
).
这篇关于C ++ OpenGL未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!