[英]OpenGL project with Codeblocks compilation issue
我正在使用Codeblocks(在具有GCC的Ubuntu中),并且已经掌握了OpenGL所需的文件,现在正在学习有关OpenGL基础的教程:
我的(基于教程的)代码:
#include <GL/glu.h>
#include <GL/freeglut.h>
//#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
}
void init()
{
/* set clear color to black */
/* glClearColor (0.0, 0.0, 0.0, 0.0); */
/* set fill color to white */
/* glColor3f(1.0, 1.0, 1.0); */
/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
/* glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); */
}
int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
/* You must call glutInit before any other OpenGL/GLUT calls */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();
}
当我尝试在代码块中运行此代码(在构建设置中设置了GCC)时,出现以下错误:
错误:对glClear错误的未定义引用:
未定义引用glBegin ...
在尝试更改构建设置几个小时后,我决定只从命令行调用GCC:gcc -o main main.c -lGL -lGLU -lglut
编译没有问题,我什至可以运行它。 所以我不确定是什么问题。 (此外,在代码块中,我确实向构建选项添加了“ -lGL -lGLU -lglut”。)
为什么命令行GCC可以编译此代码块却不能编译?
注意:在代码块中,我关闭了构建设置中的所有编译器标志。 链接器设置为空。 我添加的唯一内容是上述编译器选项。
链接器设置为空。 我添加的唯一内容是上述编译器选项
这就是问题所在:库链接是一个链接器设置。 那些-l…
必须进入链接器设置,而不是编译器设置。 同样重要的是在opengl32.lib
之前添加gdi32.lib
,以便可以找到所有符号。
在Windows上,OpenGL标头本身依赖于windows.h
定义的宏,因此您应该编写类似
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.