我正在运行Ubuntu 13.10并尝试编译该部分示例OpenGL代码:
#include "GL/freeglut.h" #include "GL/gl.h" /* display function - code from: http://fly.cc.fer.hr/~unreal/theredbook/chapter01.html This is the actual usage of the OpenGL library. The following code is the same for any platform */ void renderFunction() { glClearColor(0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glOrtho(-1.0,-1.0,1.0); glBegin(GL_POLYGON); glVertex2f(-0.5,-0.5); glVertex2f(-0.5,0.5); glVertex2f(0.5,-0.5); glEnd(); glFlush(); } /* Main method - main entry point of application the freeglut library does the window creation work for us,regardless of the platform. */ int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL - First window demo"); glutDisplayFunc(renderFunction); glutMainLoop(); return 0; }
我已经使用Eclipse来构建项目,但它在链接级别失败:
Building target: opengl_test Invoking: GCC C++ Linker g++ -L/usr/lib -L/usr/lib/fglrx -L/usr/lib/x86_64-linux-gnu -L/usr/lib/i386-linux-gnu/mesa -L/usr/lib/x86_64-linux-gnu/mesa -o "opengl_test" ./src/opengl_test.o ./src/opengl_test.o: In function `renderFunction()': /var/www/opengl_test/Debug/../src/opengl_test.cpp:10: undefined reference to `glClearColor' /var/www/opengl_test/Debug/../src/opengl_test.cpp:11: undefined reference to `glClear' /var/www/opengl_test/Debug/../src/opengl_test.cpp:12: undefined reference to `glColor3f' /var/www/opengl_test/Debug/../src/opengl_test.cpp:13: undefined reference to `glOrtho' /var/www/opengl_test/Debug/../src/opengl_test.cpp:14: undefined reference to `glBegin' /var/www/opengl_test/Debug/../src/opengl_test.cpp:15: undefined reference to `glVertex2f' /var/www/opengl_test/Debug/../src/opengl_test.cpp:16: undefined reference to `glVertex2f' /var/www/opengl_test/Debug/../src/opengl_test.cpp:17: undefined reference to `glVertex2f' /var/www/opengl_test/Debug/../src/opengl_test.cpp:18: undefined reference to `glVertex2f' /var/www/opengl_test/Debug/../src/opengl_test.cpp:19: undefined reference to `glEnd' /var/www/opengl_test/Debug/../src/opengl_test.cpp:20: undefined reference to `glFlush' ./src/opengl_test.o: In function `main': /var/www/opengl_test/Debug/../src/opengl_test.cpp:28: undefined reference to `glutInit' /var/www/opengl_test/Debug/../src/opengl_test.cpp:29: undefined reference to `glutInitDisplayMode' /var/www/opengl_test/Debug/../src/opengl_test.cpp:30: undefined reference to `glutInitWindowSize' /var/www/opengl_test/Debug/../src/opengl_test.cpp:31: undefined reference to `glutInitWindowPosition' /var/www/opengl_test/Debug/../src/opengl_test.cpp:32: undefined reference to `glutCreateWindow' /var/www/opengl_test/Debug/../src/opengl_test.cpp:33: undefined reference to `glutDisplayFunc' /var/www/opengl_test/Debug/../src/opengl_test.cpp:34: undefined reference to `glutMainLoop' collect2: error: ld returned 1 exit status make: *** [opengl_test] Error 1
……所以我试过G:
g++ -lGL -lglut opengl_test.cpp -o test
我想也许我需要手动将符号链接到文件夹/usr/lib.
我搜索过libs:
$sudo apt-file search libGL.so fglrx: /usr/lib/fglrx/libGL.so fglrx: /usr/lib/fglrx/libGL.so.1 fglrx: /usr/lib/fglrx/libGL.so.1.2 fglrx: /usr/lib32/fglrx/libGL.so.1.2 fglrx-updates: /usr/lib/fglrx/libGL.so fglrx-updates: /usr/lib/fglrx/libGL.so.1 fglrx-updates: /usr/lib/fglrx/libGL.so.1.2 fglrx-updates: /usr/lib32/fglrx/libGL.so.1.2 libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/libGL.so libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/mesa/libGL.so libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0 libgl1-mesa-glx-dbg: /usr/lib/debug/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0 nvidia-173: /usr/lib/nvidia-173/libGL.so nvidia-173: /usr/lib/nvidia-173/libGL.so.1 nvidia-173: /usr/lib/nvidia-173/libGL.so.173.14.37 nvidia-173: /usr/lib32/nvidia-173/libGL.so nvidia-173: /usr/lib32/nvidia-173/libGL.so.1 nvidia-173: /usr/lib32/nvidia-173/libGL.so.173.14.37 nvidia-304: /usr/lib/nvidia-304/libGL.so nvidia-304: /usr/lib/nvidia-304/libGL.so.1 nvidia-304: /usr/lib/nvidia-304/libGL.so.304.88 nvidia-304: /usr/lib32/nvidia-304/libGL.so nvidia-304: /usr/lib32/nvidia-304/libGL.so.1 nvidia-304: /usr/lib32/nvidia-304/libGL.so.304.88 nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.1 nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.304.108 nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.1 nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.304.108 nvidia-319: /usr/lib/nvidia-319/libGL.so nvidia-319: /usr/lib/nvidia-319/libGL.so.1 nvidia-319: /usr/lib/nvidia-319/libGL.so.319.32 nvidia-319: /usr/lib32/nvidia-319/libGL.so nvidia-319: /usr/lib32/nvidia-319/libGL.so.1 nvidia-319: /usr/lib32/nvidia-319/libGL.so.319.32 nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.1 nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.319.60 nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.1 nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.319.60 primus-libs: /usr/lib/x86_64-linux-gnu/primus/libGL.so.1
和
$sudo apt-file search libglut.so freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3 freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0 freeglut3-dev: /usr/lib/x86_64-linux-gnu/libglut.so
制作符号链接:
sudo ln -s /usr/lib/fglrx/libGL.so /usr/lib/libGL.so sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/libglut.so
但它仍然无济于事.出现了相同的链接问题.
>如何调试该问题?
>我如何知道选择哪个lib并将它们链接到/usr/lib?我选择了fglrx,因为我的GPU驱动程序正在运行fglrx.
>我是否每次都需要手动将库从/usr/lib…./somewhere链接到/usr/lib?
尝试将您的库放在源文件之后:
原文链接:https://www.f2er.com/ubuntu/347388.htmlg++ opengl_test.cpp -lGL -lglut
要使Eclipse工作,您需要更改所谓的“命令行模式”.我实际上不是Eclipse用户,但尝试在这里检查设置:C/C++ Build – >设置 – > Cross G Linker或其他一些与Linker相关的设置.
澄清:当前ld默认链接–as-needed标志.在链接任何库之前的这种模式中,ld检查这个库中的某些符号是否需要某处. ld按顺序执行此检查,因此如果您在源文件之前链接某些内容,则会排除此库.可以使用-Wl,– no-as-needed gcc标志禁用此行为.
从男人ld:
--as-needed --no-as-needed This option affects ELF DT_NEEDED tags for dynamic libraries men- tioned on the command line after the --as-needed option. Normally,the linker will add a DT_NEEDED tag for each dynamic library men- tioned on the command line,regardless of whether the library is actually needed. --as-needed causes DT_NEEDED tags to only be emitted for libraries that satisfy some symbol reference from regu- lar objects which is undefined at the point that the library was linked. --no-as-needed restores the default behavIoUr.