You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境下GLFW函数出现“undefined”引用错误求助

GLFW函数未定义引用错误解决

问题详情

运行GLFW代码时反复出现函数未定义引用错误,已将glfw3.h放置在C:\WinGW\Include\GLFW\glfw3.h,glfw3.dll放在C:\Windows\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;
}

错误输出

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

解决方法

这些错误是链接器找不到对应库文件导致的,头文件和DLL的位置没问题,但编译时没告诉链接器要关联GLFW和OpenGL的库。

  1. 确认GLFW库文件存在
    下载适配MinGW的GLFW版本,里面会有libglfw3.a(静态库)或libglfw3dll.a(动态导入库),把这个文件放到C:\MinGW\lib目录下。

  2. 编译时添加链接参数
    使用GCC编译时,必须加上对应的库链接选项:

    • 动态链接(依赖glfw3.dll):
      gcc main.c -o main.exe -lglfw3dll -lopengl32
      
    • 静态链接(不需要glfw3.dll):
      gcc main.c -o main.exe -lglfw3 -lopengl32 -lgdi32
      
  3. 额外说明

    • glClear是OpenGL函数,所以必须链接系统自带的opengl32库;静态链接GLFW时还需要额外链接gdi32库处理窗口相关功能。
    • 动态链接时,确保glfw3.dll在可执行文件同目录或C:\Windows\System32里,程序运行时才能找到它。

内容的提问来源于stack exchange,提问作者pigeoni2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:08:33