Win vscode 配置OpenGL时 undefined reference to `glfwInit'
- Win vscode 配置OpenGL时 undefined reference to `glfwInit'
- 现象
- 原因
- 解决方案
vscode_OpenGL_undefined_reference_to_glfwInit_2">Win vscode 配置OpenGL时 undefined reference to `glfwInit’
现象
win 上面用vscode 配置OpenGL 时会报一下错误
g++ -std=c++17 -Wall -Wextra -g -Iinclude -o output\main.exe src/main.o -Llib -lglad -lglfw3
src/main.o: In function `main':
D:\workspace\c\2024\opengl/src/main.cpp:14: undefined reference to `glfwInit'
D:\workspace\c\2024\opengl/src/main.cpp:15: undefined reference to `glfwWindowHint'
D:\workspace\c\2024\opengl/src/main.cpp:16: undefined reference to `glfwWindowHint'
D:\workspace\c\2024\opengl/src/main.cpp:17: undefined reference to `glfwWindowHint'
D:\workspace\c\2024\opengl/src/main.cpp:19: undefined reference to `glfwCreateWindow'
D:\workspace\c\2024\opengl/src/main.cpp:23: undefined reference to `glfwTerminate'
D:\workspace\c\2024\opengl/src/main.cpp:26: undefined reference to `glfwMakeContextCurrent'
D:\workspace\c\2024\opengl/src/main.cpp:27: undefined reference to `glfwSetFramebufferSizeCallback'
D:\workspace\c\2024\opengl/src/main.cpp:29: undefined reference to `glfwGetProcAddress'
D:\workspace\c\2024\opengl/src/main.cpp:35: undefined reference to `glfwWindowShouldClose'
D:\workspace\c\2024\opengl/src/main.cpp:38: undefined reference to `glfwSwapBuffers'
D:\workspace\c\2024\opengl/src/main.cpp:39: undefined reference to `glfwPollEvents'
D:\workspace\c\2024\opengl/src/main.cpp:42: undefined reference to `glfwTerminate'
src/main.o: In function `Z12processInputP10GLFWwindow':
D:\workspace\c\2024\opengl/src/main.cpp:48: undefined reference to `glfwGetKey'
D:\workspace\c\2024\opengl/src/main.cpp:49: undefined reference to `glfwSetWindowShouldClose'
collect2.exe: error: ld returned 1 exit status
Makefile:78: recipe for target 'main.exe' failed
make: *** [main.exe] Error 1
原因
是因为vscode
中使用了MinGW
中g++
作为项目的编译器,和下载的预编译glfw-3.4.bin.WIN64\lib-mingw-w64
中的libglfw3.a
不兼容导致的。
解决方案
下载glfw源码,使用MinGW
中的g++自己编译出来的libglfw3.a
放进自己项目的 lib
文件夹中