我是編程和學習使用Sublime Text 2中的C++編寫代碼的新手。我有JavaScript經驗,現在我正在使用Sublime Text 2編譯C++代碼。它使用普通的C++代碼運行得很好,但是使用OpenGL它會返回問題。我在MinGW中安裝了GLUT。對C++和OpenGL使用Sublime Text 2
例如: 我發現從YouTube視頻這個簡單的代碼:http://www.youtube.com/watch?v=SAmD_Aq1Un4
#include <GL/GLUT.h>
#include <iostream>
void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Simple GLUT Application");
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
void keyboard(unsigned char c, int x, int y) {
if (c == 27) {
exit(0);
}
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_RIGHT_BUTTON) {
exit(0);
}
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(-0.5, -0.5);
glColor3f(0, 1, 0);
glVertex2f(0.5, -0.5);
glColor3f(0, 0, 1);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers();
}
當我建立的文件,它說以下內容:
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1c): undefined reference to `__glutInitWithExit'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x37): undefined reference to `__glutCreateWindowWithExit'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x52): undefined reference to `__glutCreateMenuWithExit'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x80): undefined reference to `glutInitDisplayMode'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x94): undefined reference to `glutInitWindowPosition'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0xa8): undefined reference to `glutInitWindowSize'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0xc0): undefined reference to `glutDisplayFunc'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0xcc): undefined reference to `glutKeyboardFunc'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0xd8): undefined reference to `glutMouseFunc'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0xdd): undefined reference to `glutMainLoop'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x130): undefined reference to `_imp__glClear'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x13e): undefined reference to `_imp__glBegin'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x15f): undefined reference to `_imp__glColor3f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x177): undefined reference to `_imp__glVertex2f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x198): undefined reference to `_imp__glColor3f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1b0): undefined reference to `_imp__glVertex2f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1d1): undefined reference to `_imp__glColor3f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1e9): undefined reference to `_imp__glVertex2f'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1f0): undefined reference to `_imp__glEnd'
C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o:openGL_Glut.cpp:(.text+0x1f7): undefined reference to `glutSwapBuffers'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\HARIOM~1\AppData\Local\Temp\cc9WOgBg.o: bad reloc address 0x0 in section `.ctors'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s with exit code 1]
這是一個常見問題。它很快被Google發現。您必須將OpenGL和GLUT庫添加到鏈接器標誌。 – datenwolf
感謝您的回覆,但您指的是哪種常見問題,以及鏈接器標誌是什麼,請您詳細說明。感謝 – Surfer
常見問題解答,如「常見問題解答」。這不是一個特定的FAQ列表,而是一個經常被問到的問題。如果你不知道什麼是鏈接器標記,我強烈建議你閱讀GCC工具鏈的精細手冊:http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html(MinGW是GCC Windows工具鏈)。 – datenwolf