Windows 使用GLUT和MinGW的未定义引用

Windows 使用GLUT和MinGW的未定义引用,windows,opengl,mingw,glut,static-linking,Windows,Opengl,Mingw,Glut,Static Linking,下面显示的初始命令在编译用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\Loca

下面显示的初始命令在编译用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:(下载链接为“我的GLUT版本”)

  • 如果您介意的话,C程序是在

  • 我安装了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
    

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

    您下载的glut库是为32位程序构建的。看起来您的MinGW是为64位目标构建的,该目标使用的名称与32位不同(而且无论如何都不兼容)

    尝试添加-m32选项以使MinGW生成32位程序:

    gcc -m32 -Wall -ofoo mycube.c -L"C:\MinGW\lib" -lglut32cu -lglu32 -lopengl32
    
    请注意,mt 64位MinGW编译器(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>
    
    #包含或#包含
    

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

    我猜您是从一个旧教程开始的,但您使用的是64位windows。试试MinGW 64位:这很有效!谢谢你的解释。在代码中添加“#include”(正如另一个答案中所建议的那样)似乎也能解决这个问题,尽管我不知道为什么。@razzendahcuben:要获得一些APIEXPORT宏,必须添加windows.h作为标题。还要知道,即使在64位窗口中,OpenGL库也被称为OpenGL32.DLL…谢谢Dinesh,添加了“γ”包含也解决了问题。在修复之前,我得到了一个错误,比如“代码> GL/GLUT。H:43:24:错误:C++声明的内置类型“W查尔特特”[-fPurime] 。愚蠢的是,我试图编辑头文件(注释
    typedef unsigned short wchar\u t
    ),但得到了相同的链接错误。。。应该把它作为windows的线索。谢谢,在文件顶部添加#include解决了这个问题。同样,它需要放在文件的第一行。@cadsharp您使用哪个ide?代码块???