[英]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.