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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14411960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:12:20  来源:igfitidea点击:

Undefined references using GLUT with MinGW

windowsopenglmingwglutstatic-linking

提问by cadsharp

The initial command shown below yields the following errors while compiling an OpenGL program written in C.

下面显示的初始命令在编译用 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

I am aware that this question has been asked before in threads titled "Undefined references using GLUT" and "GLUT program link error".

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

Unfortunately, much of their jargon is above me so I am not quite sure how to apply their solutions. Second, given the dozens of possible configurations depending on the OS, the compiler, the version of GLUT, etc, I am not even certain if their scenario relates to mine. Here's my scenario:

不幸的是,他们的大部分术语都在我之上,所以我不太确定如何应用他们的解决方案。其次,鉴于取决于操作系统、编译器、GLUT 版本等的数十种可能的配置,我什至不确定它们的场景是否与我的场景有关。这是我的场景:

  • Using Windows 7 x64

  • 使用 Windows 7 x64

  • Using GLUT as provided here: http://www.prinmath.com/csci5229/misc/install.html(download link is "my version of GLUT")

  • 使用此处提供的 GLUT:http: //www.prinmath.com/csci5229/misc/install.html(下载链接是“我的 GLUT 版本”)

  • The C program, should you care, is the one given at http://www.wikihow.com/Make-a-Cube-in-OpenGL

  • 你应该关心的 C 程序是http://www.wikihow.com/Make-a-Cube-in-OpenGL

  • I have MinGW installed, and the "gcc" command works; I have already successfully compiled a simple 'Hello World' C program

  • 我安装了 MinGW,并且“gcc”命令有效;我已经成功编译了一个简单的“Hello World”C 程序

  • The header file is located in C:\MinGW\include\GL\glut.h

  • 头文件位于 C:\MinGW\include\GL\glut.h

  • I placed glut32.dll in C:\Windows\system32 and C:\Windows\SysWOW64

  • 我将 glut32.dll 放在 C:\Windows\system32 和 C:\Windows\SysWOW64

  • libglut32cu.a is in C:\MinGW\lib, C:\MinGW\usr\lib, C:\MinGW\usr\lib64. I know it only needs to be one, technically, but I tried all three hoping one of them would work. The instructions at the aforementioned PrinMath site suggest trying the latter two folders, it appears, when my error occurs. He also suggests using the "-L flag to tell the linker where it is". So I have tried entering this in the command prompt:

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

    Unfortunately, the result is the same.

  • 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
    

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

  • 采纳答案by Michael Burr

    The glut library you downloaded was built for 32-bit programs. It looks like your MinGW is building for a 64-bit target, which uses different names than the 32-bit (and is incompatible anyway).

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

    Try adding the -m32 option to get MinGW to builds a 32-bit program:

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

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

    Note that mt 64-bit MinGW compiler (gcc version 4.6.1 (tdm64-1) from http://tdm-gcc.tdragon.net/download) gives a small clue about this problem:

    请注意,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
    

    回答by Dinesh Subedi

    At the top of the source file mycube.c you have to include

    在源文件 mycube.c 的顶部,您必须包含

    #include<windows.h>
    

    or include

    或包括

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

    Then compile you code there will be no such type of error.

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