在 MinGW 上链接 GLFW 时出现未定义的参考错误 [英] Undefined reference errors when linking GLFW on MinGW
问题描述
我正在尝试使用 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 时出现未定义的参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!