我正在尝试编译一个基本的 glfw 应用程序。我的项目结构如下所示:
project/
main.cpp
glfw/
glfw3.h
glfw3.lib
main.cpp代码(直接来自glfw文档)
#include <glfw3.h>
int main(void)
{
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
我运行此命令来编译它:
g++ main.cpp -o main.exe -Lglfw -Iglfw -lopengl32 -lglfw3
但是,我收到此错误:
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0x17): undefined reference to `glfwInit'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0x64): undefined reference to `glfwTerminate'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
C:\Users\x\AppData\Local\Temp\ccqBTKoM.o:main.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
我尝试了 StackOverflow 和 YouTube 上的很多东西,但似乎没有一个对我的情况有帮助。
我让它工作了 - 我卸载了所有 MinGW 的东西并使用 MSYS2 安装程序重新安装了它。
g++ main.cpp -o main.exe -Lglfw -Iglfw -lglfw3 -lopengl32 -lgdi32