在 MinGW 上链接 GLFW 时出现未定义的参考错误

编程入门 行业动态 更新时间:2024-10-24 13:20:16
本文介绍了在 MinGW 上链接 GLFW 时出现未定义的参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 minGW 在 Windows 上开发带有 GLEW 和 GLFW 的 openGL 应用程序.在当前目录 project/ 中,我有目录 src/、bin/ 和 glfw-3.0.4.bin.WIN64/.我有文件 test.cpp、glew.h、glew.c 和 wglew.h 在src/ 目录.

I am trying to develop an openGL application with GLEW and GLFW on Windows using minGW. In the current directory, project/, I have the directories src/, bin/, and glfw-3.0.4.bin.WIN64/. I have the files test.cpp, glew.h, glew.c, and wglew.h in the src/ directory.

目录./glfw-3.0.4.bin.WIN64/include/包含GLFW/glfw3.h头文件.

目录./glfw-3.0.4.bin.WIN64/lib-mingw/包含glfw3.dll、glfw3dll.a, 和 libglfw3.a.

我的主文件,test.cpp 包含,

#include "glew.h" #include "GLFW/glfw3.h" #include <stdio.h> int main(int argc, char** argv) { printf("Hello, World! "); glewInit(); glfwInit(); }

我正在从 project/ 目录中通过运行编译程序(为了可读性分成两行)

I am compiling the program from the project/ directory by running (split into two lines for readability)

gcc -DGLEW_STATIC -DGLFW_DLL -o ./bin/test ./src/*.cpp ./src/glew.c -I ./glfw-3.0.4.bin.WIN64/include/ -L ./glfw-3.0.4.bin.WIN64/lib-mingw/ -lglfw3 -lopengl32

我收到以下错误:

undefined reference to `_imp_glfwInit'

我认为问题与我错误地链接 GLFW 库有关.据我了解,包括编译器选项 -lglfw3 将告诉 gcc 链接 ./glfw-3.0.4.bin.WIN64/lib-mingw/glfw3.dll,其中包含 glfwInit() 的定义.

I think the problem has to do with me linking the GLFW library incorrectly. From what I understand, including the compiler option -lglfw3 will tell gcc to link ./glfw-3.0.4.bin.WIN64/lib-mingw/glfw3.dll, which contains the definition for glfwInit().

我查看了与我类似的其他问题的解决方案,他们建议将 dll 文件复制到源/二进制目录并更改 -l 选项的顺序,但似乎都没有解决问题我.

I've looked at solutions to other problems similar to mine and they suggest things such as copying the dll file to the source/binary directories and changing the order of the -l options, but none have seemed to solve the problem for me.

推荐答案

您的问题是 gcc 遵循严格的库命名约定.它试图找到 glfw3.dll.a,但没有找到(因为它被命名为 glfw3dll.a - 简单的重命名将解决您的问题).

Your problem is that gcc follows strict library naming conventions. It attempts to find glfw3.dll.a, but finds none (because it is named glfw3dll.a - simple rename will fix your problem).

下一步它查找 libglfw3.a,并成功 - 但它是一个静态库,而在头文件中声明为动态的引用(棘手的 Windows DECLSPECs...这个问题不存在在例如 linux 上).所以,它找不到_imp__glfwInit,因为在静态库中它只被称为glfwInit,所以你得到一个错误.

Next step it looks for libglfw3.a, and succeeds - but it is a static library, while reference declared as dynamic in header files (tricky windows DECLSPECs... this problem don't exist on e.g. linux). So, it cannot find _imp__glfwInit, because in static library it is called just glfwInit, so you getting an error.

删除 libglfw3.a 也是一种选择 - 在这种情况下,gcc 会进一步查找并最终找到 glfw3.dll 并使用它.

Removing libglfw3.a is also one of options - in that case gcc will look further and eventually find glfw3.dll and use it.

更多推荐

在 MinGW 上链接 GLFW 时出现未定义的参考错误

本文发布于:2023-11-16 18:43:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   未定义   链接   MinGW   GLFW

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!