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

面向Apple M芯片构建时出现___isPlatformVersionAtLeast链接错误

问题解决指南

一、解决___isPlatformVersionAtLeast链接错误

该符号属于macOS系统框架,未找到的核心原因是链接参数缺失或版本配置不统一,按以下步骤修复:

  1. 统一最低macOS版本与框架链接配置
    修改.cargo/config.toml,确保所有编译单元遵循macOS 11.0+要求,并显式链接必要系统框架:

    [target.aarch64-apple-darwin]
    linker = "clang"
    rustflags = [
        "-C", "link-arg=-mmacosx-version-min=11.0",
        "-C", "link-arg=-framework",
        "-C", "link-arg=Foundation",
        "-C", "link-arg=-framework",
        "-C", "link-arg=CoreFoundation"
    ]
    
  2. 简化build.rs避免重复链接
    移除build.rs中重复的框架链接指令,仅保留LibTorch相关配置(MPS版LibTorch无需单独链接torch_cpu):

    use dirs::home_dir;
    
    fn main() {
        if let Some(home_dir) = dirs::home_dir() {
            let libtorch_path = home_dir.join(".pyano").join("binaries");
            let libtorch_path_str = libtorch_path.to_str().expect("Invalid libtorch path");
    
            println!("cargo:rustc-link-search={}", libtorch_path_str);
            println!("cargo:rustc-link-arg=-Wl,-rpath,{}", libtorch_path_str);
    
            println!("cargo:rustc-link-lib=dylib=torch");
            println!("cargo:rustc-link-lib=dylib=c10");
        }
    }
    
  3. 清理缓存重新构建
    执行以下命令清除旧编译缓存,避免残留配置干扰:

    cargo clean
    cargo build --target aarch64-apple-darwin
    

二、MPS版LibTorch打包方案

方案1:利用tch-rs自动下载集成

tch-rs支持自动适配MPS后端,只需在Cargo.toml中启用对应特性:

[dependencies]
tch = { version = "0.15", features = ["mps"] }
rust-bert = "0.22"

构建时tch-rs会自动下载带MPS支持的LibTorch预编译包,无需手动管理路径。打包应用时,将tch-rs缓存目录(默认~/.cache/torch)或target目录下的LibTorch动态库复制到应用Contents/Frameworks目录,并通过rpath指定路径。

方案2:手动下载打包官方预编译包

  1. 获取适配包:下载PyTorch官方提供的aarch64-apple-darwin版本LibTorch(稳定版默认包含MPS支持),解压后得到动态库文件夹。
  2. 集成构建流程:将解压后的LibTorch文件夹放入项目目录(如./libtorch-mps),修改build.rs指定链接路径:
    use std::path::Path;
    
    fn main() {
        let libtorch_path = Path::new("./libtorch-mps");
        let libtorch_path_str = libtorch_path.to_str().expect("Invalid libtorch path");
    
        println!("cargo:rustc-link-search={}", libtorch_path_str);
        println!("cargo:rustc-link-search={}/lib", libtorch_path_str);
        println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../Frameworks");
        println!("cargo:rustc-link-lib=dylib=torch");
        println!("cargo:rustc-link-lib=dylib=c10");
    }
    
  3. 打包收尾:构建完成后,将LibTorch动态库(libtorch.dylib、libc10.dylib等)复制到应用Contents/Frameworks目录,确保rpath配置正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 17:49:52