使用MinGW和GLUT时出现未定义引用错误

9

以下是在编写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程序链接错误”的主题中被问过了。

不幸的是,他们的大部分行话超出了我的理解范围,所以我不太确定如何应用他们的解决方案。其次,鉴于可能的配置数目取决于操作系统、编译器、GLUT版本等等,我甚至不确定他们的情况是否与我的相关。以下是我的情况:

  • 使用Windows 7 x64
  • 使用此处提供的GLUT:http://www.prinmath.com/csci5229/misc/install.html(下载链接为“my version of 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网站的说明中可以看出,当我的错误发生时,他建议尝试后两个文件夹。他还建议使用“-L”标志告诉链接器它在哪里。所以我尝试在命令提示符中输入:
  • gcc -Wall -ofoo mycube.c -L"C:\MinGW\lib" -lglut32cu -lglu32 -lopengl32
    

    很遗憾,结果是一样的。

    我猜你正在使用一份旧的教程,但是你在使用64位的Windows系统。建议你尝试使用MinGW 64位代替:http://mingw-w64.sourceforge.net/ - jondinham
    2个回答

    6
    在源文件mycube.c的顶部,您必须包含以下内容:
    #include<windows.h>
    

    或者包含

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

    然后编译您的代码,就不会出现这种类型的错误。

    2
    谢谢Dinesh,添加#include<windows.h>也解决了问题。 - cadsharp
    在修复之前,我收到了一个错误消息,类似于 GL/glut.h:43:24: error: redeclaration of C++ built-in type 'wchar_t' [-fpermissive]。愚蠢地,我试图编辑头文件(将 typedef unsigned short wchar_t 注释掉),结果得到了同样的链接错误...... 应该将其视为 windows.h 的提示。 - tmrlvi
    谢谢,将#include<windows.h>添加到文件顶部解决了问题。需要注意的是,它必须放在文件的第一行。 - Pirun Seng

    4
    你下载的glut库是为32位程序构建的。看起来你的MinGW正在构建64位目标,使用与32位不同的名称(并且不兼容)。尝试添加-m32选项以使MinGW构建32位程序:
    gcc -m32 -Wall -ofoo mycube.c -L"C:\MinGW\lib" -lglut32cu -lglu32 -lopengl32
    

    注意,64位的MinGW编译器(gcc版本4.6.1(tdm64-1),从http://tdm-gcc.tdragon.net/download下载)对这个问题提供了一个小提示:
    ld.exe: skipping incompatible .\glut32\lib/libglut32cu.a when searching for -lglut32cu
    ld.exe: cannot find -lglut32cu
    

    2
    这个可以工作!感谢您的解释。将“#include<windows.h>”添加到代码中(如另一个答案所建议的)似乎也解决了问题,尽管我不确定为什么。 - cadsharp
    @razzendahcuben:将windows.h添加为头文件是获取一些APIEXPORT宏的必要步骤。同时请注意,即使在64位Windows中,OpenGL库也被称为opengl32.dll... - datenwolf

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接