GLFW 函数“未定义”(Windows)

问题描述 投票:0回答:1

出于某种原因,每当我尝试运行代码时,我都会收到相同的错误,表明我尝试使用的函数是“未定义”。

我将 glfw3.h 文件放在

C:\WinGW\Include\GLFW\glfw3.h
并将 glfw3.dll 放在
C:\Window\System32\glfw3.dll

这是我的代码:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    if (!glfwInit())
        return -1;

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

我希望它创建一个 640 x 480 像素的窗口,但我得到了这个输出:

C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x17): undefined reference to 'glfwInit'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x51): undefined reference to 'glfwCreateWindow'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x5f): undefined reference to 'glfwTerminate'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x71): undefined reference to 'glfwMakeContextCurrent'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x7f): undefined reference to '_imp__glClear@4'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x8f): undefined reference to 'glfwSwapBuffers'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x94): undefined reference to 'glfwPollEvents'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0x9f): undefined reference to 'glfwWindowShouldClose'
C:\Users\EMIELN~1\AppData\Local\Temp\ccUSxbbl.o:main.c:(.text+0xa8): undefined reference to 'glfwTerminate'
collect2.exe: error: ld returned 1 exit status```
c windows glfw
1个回答
0
投票

编辑系统目录(例如,

C:\Window\System32\
)通常不是一个好主意,但我们将忽略它来寻找这个答案。

您收到链接器错误,具体来说,该错误意味着编译器/链接器找不到您正在引用的源代码。解决这个问题就像链接实际的库一样简单。在您使用的任何构建系统(make、CMake 等)中添加相当于 -lglfw3 的内容。

对于 CMake:

# ...

cmake_minimum_required(VERSION 3.29)
project(link_dynamic_lib_project)

include_directories("C:\WinGW\Include\GLFW")
link_directories("C:\Window\System32")

add_executable(main main.c)
target_link_libraries(main PRIVATE glfw3)

# ...
© www.soinside.com 2019 - 2024. All rights reserved.