使用GLUT和MinGW的未定义引用

下面显示的初始命令在编译用C编写的OpenGL程序时产生以下错误

C:\Users\razz\Desktop>gcc -Wall -ofoo mycube.c -lglut32cu -lglu32 -lopengl32 C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1c): undefined reference to `__glutInitWithExit' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x37): undefined reference to `__glutCreateWindowWithExit' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x52): undefined reference to `__glutCreateMenuWithExit' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x66): undefined reference to `_imp__glClear' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x6d): undefined reference to `_imp__glLoadIdentity' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x9e): undefined reference to `_imp__glRotatef' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0xcf): undefined reference to `_imp__glRotatef' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0xdd): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0xfe): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x11f): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x140): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x161): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x182): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1a3): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1c4): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1e5): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1ec): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x1fa): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x21b): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x23c): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x25d): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x27e): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x29f): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x2a6): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x2b4): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x2d5): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x2f6): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x317): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x338): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x359): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x360): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x36e): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x38f): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x3b0): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x3d1): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x3f2): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x413): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x41a): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x428): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x449): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x46a): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x48b): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x4ac): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x4cd): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x4d4): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x4e2): undefined reference to `_imp__glBegin' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x503): undefined reference to `_imp__glColor3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x524): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x545): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x566): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x587): undefined reference to `_imp__glVertex3f' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x58e): undefined reference to `_imp__glEnd' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x595): undefined reference to `_imp__glFlush' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x59c): undefined reference to `glutSwapBuffers' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x617): undefined reference to `glutPostRedisplay' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x645): undefined reference to `glutInitDisplayMode' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x65d): undefined reference to `_imp__glEnable' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x66c): undefined reference to `glutDisplayFunc' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x678): undefined reference to `glutSpecialFunc' C:\Users\razz\AppData\Local\Temp\ccs833b1.o:mycube.c:(.text+0x67d): undefined reference to `glutMainLoop' collect2: ld returned 1 exit status 

我知道这个问题已经在标题为“未定义引用使用GLUT”和“GLUT程序链接错误”的线程中被询问过。

不幸的是,他们的行话大部分都在我之上,所以我不太清楚如何应用他们的解决scheme。 其次,根据操作系统,编译器,GLUT版本等等的不同,有数十种可能的configuration,我甚至不确定它们的场景是否与我的相关。 这是我的场景:

  • 使用Windows 7 x64
  • 使用这里提供的GLUT: http : //www.prinmath.com/csci5229/misc/install.html (下载链接是“我的GLUT版本”)
  • 你应该关心的C程序是在http://www.wikihow.com/Make-a-Cube-in-OpenGL中给出的
  • 我安装了MinGW,“gcc”命令有效; 我已经成功编译了一个简单的“Hello World”C程序
  • 头文件位于C:\ MinGW \ include \ GL \ glut.h
  • 我把glut32.dll放在C:\ Windows \ system32和C:\ Windows \ SysWOW64中
  • libglut32cu.a位于C:\ MinGW \ lib,C:\ MinGW \ usr \ lib,C:\ MinGW \ usr \ lib64中。 我知道在技术上只需要一个,但我试过三个人都希望其中一个能够工作。 在上述PrinMath网站上的说明build议尝试后两个文件夹,看来,当我的错误发生。 他还build议使用“-L标志告诉链接器它在哪里”。 所以我试图在命令提示符下input:
  •  gcc -Wall -ofoo mycube.c -L"C:\MinGW\lib" -lglut32cu -lglu32 -lopengl32 

    不幸的是,结果是一样的。

    您下载的大量库是为32位程序而构建的。 它看起来像你的MinGW正在建立一个64位的目标,使用不同的名称比32位(反正是不兼容)。

    尝试添加-m32选项来让MinGW建立一个32位程序:

     gcc -m32 -Wall -ofoo mycube.c -L"C:\MinGW\lib" -lglut32cu -lglu32 -lopengl32 

    请注意,mt 64位MinGW编译器(来自http://tdm-gcc.tdragon.net/download的 gcc版本4.6.1(tdm64-1))给出了一个关于这个问题的小线索:

     ld.exe: skipping incompatible .\glut32\lib/libglut32cu.a when searching for -lglut32cu ld.exe: cannot find -lglut32cu 

    在源文件mycube.c的顶部,你必须包括

     #include<windows.h> 

    或包括

     #include<GL/gl.h> or #include<GL/glu.h> 

    那么编译你的代码就不会有这种类型的错误了。