在ubuntu中编译GLUT时出错
Error with GLUT compile in ubuntu
我试图编译一些"hello-world"供过于求的应用程序:
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
GLint Width = 512, Height = 512;
const int CubeSize = 200;
void Display(void)
{
int left, right, top, bottom;
left = (Width - CubeSize) / 2;
right = left + CubeSize;
bottom = (Height - CubeSize) / 2;
top = bottom + CubeSize;
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(255,0,0);
glBegin(GL_QUADS);
glVertex2f(left,bottom);
glVertex2f(left,top);
glVertex2f(right,top);
glVertex2f(right,bottom);
glEnd();
glFinish();
}
void Reshape(GLint w, GLint h)
{
Width = w;
Height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Keyboard(unsigned char key, int x, int y)
{
#define ESCAPE ' 33'
if( key == ESCAPE )
exit(0);
}
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(Width, Height);
glutCreateWindow("Red square example");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop();
}
编译命令为:
gcc -lGL -lGLU hw_opengl.cpp -o hw_opengl
我有以下错误:
/tmp/ccbnBFHC.o: In function `Display()':
hw_opengl.cpp:(.text+0x6c): undefined reference to `glClearColor'
hw_opengl.cpp:(.text+0x78): undefined reference to `glClear'
hw_opengl.cpp:(.text+0x94): undefined reference to `glColor3ub'
hw_opengl.cpp:(.text+0xa0): undefined reference to `glBegin'
hw_opengl.cpp:(.text+0xb4): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xc8): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xdc): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xf0): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xf5): undefined reference to `glEnd'
hw_opengl.cpp:(.text+0xfa): undefined reference to `glFinish'
/tmp/ccbnBFHC.o: In function `Reshape(int, int)':
hw_opengl.cpp:(.text+0x134): undefined reference to `glViewport'
hw_opengl.cpp:(.text+0x140): undefined reference to `glMatrixMode'
hw_opengl.cpp:(.text+0x145): undefined reference to `glLoadIdentity'
hw_opengl.cpp:(.text+0x173): undefined reference to `glOrtho'
hw_opengl.cpp:(.text+0x17f): undefined reference to `glMatrixMode'
hw_opengl.cpp:(.text+0x184): undefined reference to `glLoadIdentity'
/tmp/ccbnBFHC.o: In function `main':
hw_opengl.cpp:(.text+0x1c1): undefined reference to `glutInit'
hw_opengl.cpp:(.text+0x1cd): undefined reference to `glutInitDisplayMode'
hw_opengl.cpp:(.text+0x1e4): undefined reference to `glutInitWindowSize'
hw_opengl.cpp:(.text+0x1f0): undefined reference to `glutCreateWindow'
hw_opengl.cpp:(.text+0x1fc): undefined reference to `glutDisplayFunc'
hw_opengl.cpp:(.text+0x208): undefined reference to `glutReshapeFunc'
hw_opengl.cpp:(.text+0x214): undefined reference to `glutKeyboardFunc'
hw_opengl.cpp:(.text+0x219): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
我安装了GLUT:sudo apt-get安装freeglut3freeglut3-dev
在/usr/lib中有:libglut.a
libglut.so
libglut.so.3
libglut.so.3.9.0
locate glu.h
/home/goran/QtSDK/QtSources/4.8.0/src/3rdparty/webkit/Source/ThirdParty/glu/internal_glu.h
/usr/include/GL/glu.h
/usr/include/GL/gl.h
locate gl.h
...
/usr/include/GL/gl.h
我做错了什么?
GCC链接器可能会按照库在命令行上的顺序扫描库,这意味着它可能会先扫描库,但看不到有人在使用它们,因此会出现错误。可以肯定的是,将库放在命令行的最后:
gcc hw_opengl.cpp -o hw_opengl -lGL -lGLU -lglut
g++ filename.cpp -lGL -lglut -o exfilename
然后
./exfilename
这是因为xlibmesa-gl-dev和xlibmesaglu-dev没有软链接到文件libGL.so和libGLU.so,所以ld找不到它们来与代码链接。
您缺少-lglut
。
正确的编译命令是gcc -lGL -lglut -lGLU hw_opengl.cpp -o hw_opengl
检查您是否安装了binutils-gold
(黄金链接器(,如果没有,请安装它,如果您已经安装了,请将其删除并在终端上尝试
g++ sourcefile.cpp -lglut
g++ -lglut -lGLU -lGL helloWorld.cpp -o helloWorld
g++
,而不是gcc
,用于cpp
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- GLUT 问题:重新声明 c++ 内置类型'wchar_t'时出错
- 使用 GLUT 程序编译时出错
- 在ubuntu中编译GLUT时出错