我是在 Sublime Text 2 中通过 C++ 使用 OpenGL 编程和学习编码的新手。我有 javascript 经验,现在我正在使用带有 MinGW 的 Sublime Text 2 来编译 C++ 代码。它使用普通的 C++ 代码运行得很好,但是使用 OpenGL 它会返回问题。我已经在 MinGW 中安装了 GLUT。
例如:我从 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]