C++ Ubuntu 13.10c++;OpenGL GLUT-链接问题-未定义对“glClearColor';
我正在运行Ubuntu 13.10,并试图编译OpenGL示例代码的这一部分:C++ Ubuntu 13.10c++;OpenGL GLUT-链接问题-未定义对“glClearColor';,c++,eclipse,opengl,ubuntu,freeglut,C++,Eclipse,Opengl,Ubuntu,Freeglut,我正在运行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 th
#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, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
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
然而,它给出的输出与Eclipse的输出几乎相同
我想也许我需要将必要的lib手动符号链接到/usr/lib。
我搜索了LIB:
$ 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 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
然而,这仍然没有帮助。同样的链接问题也出现了
尝试将库放在源文件之后:
g++ opengl_test.cpp -lGL -lglut
要使Eclipse正常工作,您需要更改所谓的“命令行模式”。我实际上不是Eclipse用户,但请尝试检查此处的设置:C/C++构建->设置->交叉G++链接器
,或其他一些与链接器相关的设置
说明:当前的ld
默认链接为——根据需要
标志。在此模式下,在链接任何库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.
真遗憾。无法使用Google找到答案,但通过Stackoverflow上的相关面板找到了答案:仍然需要找出Eclipse失败的原因,但这是Eclipse本身相关的。是的,这做得很好。看到我上面的评论,同时找到了答案。无论如何,谢谢。@NeverdingQueue正在为Eclipse寻找解决方案。请检查我的编辑。我已经成功地使用Eclipse进行了构建,但我必须手动添加库:GL glut我想我可以只添加/usr/lib,直接在该文件夹中添加的所有库都将可用。当我添加该路径时,Eclipse尝试使用-L/usr/lib构建,但它会导致相同的链接器问题。请尝试传递
-Wl,--no-as-needed
标志。感谢您对--as-needed行为的解释。我的Eclipse问题是我没有单独包含这些库。当我指定它们时,Eclipse创建了正确的命令顺序,所以,--no-as-needed标志现在不需要。我只是想,如果我通过-L标志,它将获得所有必要的LIB。-L只是应该在链接器搜索的dir路径,您仍然需要使用-L标志来给出库。无论如何,谢谢你的积极性。
--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.