Undefined reference errors when linking GLFW on MinGW

天涯浪子 提交于 2019-11-28 04:52:17

问题


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.

The directory ./glfw-3.0.4.bin.WIN64/include/ contains the GLFW/glfw3.h header file.

The directory ./glfw-3.0.4.bin.WIN64/lib-mingw/ contains glfw3.dll, glfw3dll.a, and libglfw3.a.

My main file, test.cpp contains,

#include "glew.h"
#include "GLFW/glfw3.h"

#include <stdio.h>

int main(int argc, char** argv) {
    printf("Hello, World!\n");

    glewInit();
    glfwInit();
}

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

and I am getting the following error:

undefined reference to `_imp_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().

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.


回答1:


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).

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.

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



来源:https://stackoverflow.com/questions/22623087/undefined-reference-errors-when-linking-glfw-on-mingw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!