OpenGL - 对GLUT的未定义引用

时间:2015-02-28 20:51:32

标签: c windows opengl mingw

我目前在使用MinGW在Windows 7上运行openGL时遇到问题。我按照此处的步骤进行操作:https://www.opengl.org/wiki/MinGW

我安装了MinGW及其所有基本安装文件。我已经添加了PATH变量所需的元素。我已经下载并将GLUT文件放在正确的目录中,如上面的链接所述。即便如此,使用以下代码我会得到后续错误。

#include <GL/glut.h>

void display (void) 
{
    glClear (GL_COLOR_BUFFER_BIT);

    glBegin (GL_POLYGON);
      glVertex2f (-0.5, -0.5);
      glVertex2f (-0.5, 0.5);
      glVertex2f (0.5, 0.5);
      glVertex2f (0.5, -0.5);
    glEnd ();

    glFlush ();
    return;
}

int main (int argc, char **argv) 
{
    glutInit (&argc, argv);
    glutCreateWindow ("simple");
    glutDisplayFunc (display);
    glutMainLoop ();
}

用于编译该程序的命令是: gcc firstProg.c -o firstProg.exe glut32.lib -lopengl32 -lglu32

错误如下(取自cmd):

C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x1c): undefined
 reference to `__glutInitWithExit'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x37): undefined
 reference to `__glutCreateWindowWithExit'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x52): undefined
 reference to `__glutCreateMenuWithExit'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x66): undefined
 reference to `_imp__glClear'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x74): undefined
 reference to `_imp__glBegin'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x8c): undefined
 reference to `_imp__glVertex2f'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0xa4): undefined
 reference to `_imp__glVertex2f'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0xbc): undefined
 reference to `_imp__glVertex2f'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0xd4): undefined
 reference to `_imp__glVertex2f'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0xdb): undefined
 reference to `_imp__glEnd'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0xe2): undefined
 reference to `_imp__glFlush'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x11f): undefine
d reference to `glutDisplayFunc'
C:\Users\Dylan\AppData\Local\Temp\cciCuMP6.o:firstProg.c:(.text+0x124): undefine
d reference to `glutMainLoop'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\D
ylan\AppData\Local\Temp\cciCuMP6.o: bad reloc address 0x20 in section `.eh_frame
'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

这里的任何人都知道如何解决这些错误?我已经尝试卸载并重新安装MinGW,并将GLUT文件再次放在所需的目录中,但无济于事。

1 个答案:

答案 0 :(得分:1)

步骤1:确保您有一个与编译器工具链匹配的正确构建的GLUT库。

步骤2:实际上 GLUT添加到要链接到的库列表中。

GLUT是第三方库,不属于OpenGL(无论如何)。