【发布时间】:2015-07-14 05:06:35
【问题描述】:
在我擦除硬盘驱动器并重新安装操作系统之前,我有一个可以在我的计算机上运行的程序。我重新安装了所有必要的编译器和 DLL,但我收到了 glut 的链接器错误。下面是我尝试运行的代码的 sn-p 以及我的编译命令是什么。
这是我要运行的主要内容:
/*
* GL01Hello.cpp: Test OpenGL C/C++ Setup
*/
#include <windows.h> // For MS Windows
#include <GL/freeglut.h> // GLUT, includes glu.h and gl.h
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the infinitely event-processing loop
return 0;
}
这是正在运行的命令:
g++ GL01Hello.cpp -o GL01Hello.exe -lglu32 -lopengl32 -lfreeglut
我得到的错误都在下面:
g++ GL01Hello.cpp -o GL01Hello.exe -lglu32 -lopengl32 -lfreeglut
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x3e): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x60): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x198): undefined reference to `_imp__glutInitWindowSize@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1b1): undefined reference to `_imp__glutInitWindowPosition@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1c2): undefined reference to `_imp__glutDisplayFunc@4'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1cc):
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.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
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
解决这些链接器错误的任何帮助都会非常有帮助。
编辑:我将上面的所有代码都编辑为一个非常简单的示例,但我仍然得到一个非常相似的链接器错误。有人将此报告为与通用“导致链接器错误的原因”帖子类似的问题,我已经尝试了其中的许多,但没有一个有帮助。谢谢你的尝试
【问题讨论】:
-
很可能问题不仅仅在于您的主要功能。如果我不得不猜测,这与包含
glut.h、glew.h的顺序有关,而且——因为它存在链接器错误——“glm.h”。 GLUT 和 GLEW 都在用宏做一些很时髦的东西,以避免命名空间污染。但如果事情纠缠不清,它也可能导致链接错误。尝试将您的代码简化为仍然暴露问题的最小示例并发布完整的代码。 -
感谢您的回复。我将代码更改为一个非常简单的示例。任何帮助将不胜感激。
标签: c++ opengl linker mingw freeglut