面向Apple M芯片构建时出现___isPlatformVersionAtLeast链接错误
问题解决指南
一、解决___isPlatformVersionAtLeast链接错误
该符号属于macOS系统框架,未找到的核心原因是链接参数缺失或版本配置不统一,按以下步骤修复:
统一最低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" ]简化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"); } }清理缓存重新构建
执行以下命令清除旧编译缓存,避免残留配置干扰: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:手动下载打包官方预编译包
- 获取适配包:下载PyTorch官方提供的
aarch64-apple-darwin版本LibTorch(稳定版默认包含MPS支持),解压后得到动态库文件夹。 - 集成构建流程:将解压后的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"); } - 打包收尾:构建完成后,将LibTorch动态库(
libtorch.dylib、libc10.dylib等)复制到应用Contents/Frameworks目录,确保rpath配置正确。
内容的提问来源于stack exchange,提问作者GraphicalDot
相关产品推荐
相关产品推荐

