我是编程和学习使用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