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

MacOS下编译GLFW遇链接错误,求解决方法

链接GLFW时出现Undefined symbols错误的解决方法

问题背景

尝试使用Premake搭建项目,链接GLFW时出现以下链接错误:

ld: Undefined symbols:
  _OBJC_CLASS_$_CAMetalLayer, referenced from:
       in libGLFW.a[6](cocoa_window.o)
  __glfwSelectPlatform, referenced from:
      _glfwInit in libGLFW.a[9](init.o)

相关Premake文件如下:

GLFW库的Premake文件

project "GLFW"
    kind "StaticLib"
    language "C"

    targetdir ("%{wks.location}/bin/%{cfg.platform}_%{cfg.buildcfg}/%{prj.name}")
    objdir ("%{wks.location}/bin-int/%{cfg.platform}_%{cfg.buildcfg}/%{prj.name}")

    -- support only for MacOS

    files {
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
        "src/glfw_config.h",
        "src/context.c",
        "src/init.c",
        "src/input.c",
        "src/monitor.c",
        "src/vulkan.c",
        "src/window.c",
        "src/egl_context.c",
        "src/osmesa_context.c",
        "src/cocoa_time.c",
        "src/posix_module.c",
        "src/posix_thread.c",
        "src/cocoa_init.m",
        "src/cocoa_joystick.m",
        "src/cocoa_monitor.m",
        "src/cocoa_window.m",
        "src/nsgl_context.m"
    }

    defines {
        "_GLFW_COCOA"
    }

工作区及项目的Premake文件

workspace "GLFWWindowWorkspace"
    architecture "ARM64"

    configurations {
        "Debug",
        "Release"
    }

    IncludeDir = {}
    IncludeDir["glfw"] = "%{wks.location}/vendor/GLFW/include"

    group "Dependencies"
        include "vendor/GLFW"

    group "Core"
        project "GLFWWindow"
        kind "ConsoleApp"
        language "C++"
        cppdialect "C++20"

        targetdir ("%{wks.location}/bin/%{cfg.platform}_%{cfg.buildcfg}/%{prj.name}")
        objdir ("%{wks.location}/bin-int/%{cfg.platform}_%{cfg.buildcfg}/%{prj.name}")

        files {
            "src/**.h",
            "src/**.hpp",
            "src/**.cpp"
        }

        includedirs {
            "%{IncludeDir.glfw}"
        }
      
        links {
            "OpenGL.framework",
            "Cocoa.framework",
            "IOKit.framework",
            "CoreFoundation.framework",
            "GLFW"
        }

解决步骤

1. 添加Metal.framework依赖

_OBJC_CLASS_$_CAMetalLayer 是Metal框架中的类,需要将Metal框架加入项目的链接列表中。修改工作区项目的links配置:

links {
    "OpenGL.framework",
    "Cocoa.framework",
    "IOKit.framework",
    "CoreFoundation.framework",
    "Metal.framework", -- 新增Metal框架
    "GLFW"
}

2. 补全GLFW缺失的平台实现文件

__glfwSelectPlatform 未定义是因为GLFW项目中缺少了实现该函数的平台文件。在GLFW的Premake文件的files列表中添加以下文件(确保这些文件存在于你的GLFW源码目录中):

"src/cocoa_platform.h",
"src/cocoa_platform.m"

修改后的GLFW项目files部分示例:

files {
    "include/GLFW/glfw3.h",
    "include/GLFW/glfw3native.h",
    "src/glfw_config.h",
    "src/context.c",
    "src/init.c",
    "src/input.c",
    "src/monitor.c",
    "src/vulkan.c",
    "src/window.c",
    "src/egl_context.c",
    "src/osmesa_context.c",
    "src/cocoa_time.c",
    "src/posix_module.c",
    "src/posix_thread.c",
    "src/cocoa_init.m",
    "src/cocoa_joystick.m",
    "src/cocoa_monitor.m",
    "src/cocoa_window.m",
    "src/nsgl_context.m",
    "src/cocoa_platform.h", -- 新增
    "src/cocoa_platform.m"  -- 新增
}

完成以上修改后,重新生成项目文件并编译,即可解决这两个未定义符号的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 13:59:55