使用MinGW编译OpenGL并得到了过剩链接器错误
Using MinGW to compile OpenGL and getting glut Linker errors
在我清空硬盘并重新安装操作系统之前,我的电脑上有一个可以运行的程序。我重新安装了所有必要的编译器和dll,但我得到了过剩的链接器错误。下面是我试图运行的代码片段,以及我的编译命令是什么。
下面是我要运行的主程序:
/*
* 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:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x3e): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x60): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x198): undefined reference to `_imp__glutInitWindowSize@8'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x1b1): undefined reference to `_imp__glutInitWindowPosition@8'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x1c2): undefined reference to `_imp__glutDisplayFunc@4'
C:UsersDanielAppDataLocalTempccC01yPe.o:GL01Hello.cpp:(.text+0x1cc):
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:UsersDanielAppDataLocalTempccC01yPe.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
任何解决这些链接器错误的帮助都是非常有帮助的。
编辑:我编辑上面的所有代码是一个非常简单的例子,我仍然得到一个非常相似的链接错误。有人报告说,这是一个类似的问题,一般的"什么原因导致链接错误"的帖子,我已经尝试了许多这些,没有一个有帮助。谢谢你的努力
你做的每件事看起来都很好。我最好的打赌是,你正在使用的FreeGLUT的构建(特别是.def
和.lib
链接器存根)不匹配(也可能是.dll
)。我的建议是,使用您用来构建程序的编译器从源代码构建FreeGLUT。尝试一下,并报告任何差异。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误