首页 > 解决方案 > 如何为库指定 macOS 部署目标?

问题描述

我正在为 macOS 构建一个 AU 音频插件,该插件链接到使用 Cargo 编译的静态 Rust 库。我通过 CMake 编译我的插件,并将 macOS 部署目标设置为 10.9。在链接时,我收到大量错误,例如

ld: warning: object file (my/rust/libfoo.a (std-bd716fa574aff005.std.8vdpzfpj-cgu.15.rcgu.o)) was built for newer macOS version (10.15) than being linked (10.9)

运行 MacOS 10.13 的测试人员确认加载插件会导致崩溃。

我还需要为 10.9 的 macOS 部署目标编译 Rust 库,但我该怎么做呢?

我正在编译的库是 resvg,我修改了项目Cargo.toml以在我自己的 fork 中输出一个静态库,您可以在此处找到。

我调用cargo build --release并编译了我的静态库。

跑步

otool -l libresvg.a | rg LC_VERSION_MIN_MACOSX -A2 | sort | uniq

输出

  cmdsize 16
  version 10.15
  version 10.7

标签: rustrust-cargo

解决方案


对于 x86 和 x86_64 目标, Rust默认以 macOS 10.7为目标(aarch64 目标为 11.0):

% cargo new --lib demo

% cd demo

% echo '[lib]' >> Cargo.toml

% echo 'crate-type = ["staticlib"]' >> Cargo.toml

% unset MACOSX_DEPLOYMENT_TARGET

% cargo build

# Older versions of macOS and/or x86 and x86_64
% otool -l target/debug/libdemo.a | rg LC_VERSION_MIN_MACOSX -A2 | rg version | sort | uniq -c
 198   version 10.7

# Newer versions of macOS and/or aarch64
% otool -l target/debug/libdemo.a | rg LC_BUILD_VERSION -A4 | rg minos | sort | uniq -c

您可以通过指定环境变量来选择支持较新版本:MACOSX_DEPLOYMENT_TARGET

% export MACOSX_DEPLOYMENT_TARGET=10.12

% cargo clean && cargo build

% otool -l target/debug/libdemo.a | rg LC_VERSION_MIN_MACOSX -A2 | rg version | sort | uniq -c
   2   version 10.12
 196   version 10.7

如果您需要针对10.7之前的版本,我希望您必须重新编译 Rust 标准库本身。


推荐阅读