0

私はプログラミングの初心者であり、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]
4

1 に答える 1

0

私は c++ です。(この例では静的) ライブラリを使用する場合は、それに対してリンクする必要があります。通常の英語では、ライブラリの関数の実装を見つけることができる場所をコンパイラに伝える必要があります。エディターでそれを行う方法はわかりませんが、通常、リンカー設定と呼ばれるものがあり、プロジェクトに必要なすべてのライブラリをリンクできます。詳細については、この wiki ページを参照してください

于 2013-06-03T14:28:22.113 に答える