使用Sublime Text 2进行C ++和OpenGL

时间:2013-06-03 13:27:14

标签: c++ opengl glut sublimetext

我是编程和学习用Sublime Text 2中的C ++编写OpenGL代码的新手。我有javascript经验,我现在使用Sublime Text 2和MinGW来编译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]

1 个答案:

答案 0 :(得分:0)

我是c ++,如果你想使用(在这个例子中是静态的)库,你必须链接它。在通常的英语中,您必须告诉编译器它可以在哪里找到库函数的实现。我不知道如何在您的编辑器中执行此操作,但通常会有一些称为链接器设置的内容,您可以在其中链接项目所需的所有库。有关详细信息see this wiki page