Rust PyO3 与 cc 链接失败

Pat*_*Pat 2 rust

我正在使用 Cargo build 编译 pyo3 示例代码。我在最后看到这个错误

ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

其他代码编译良好。仅当我使用 pyo3 时才看到此错误。

我使用的是配备 M1 芯片的 MacBook。我已经安装了 Xcode。Rust 工具链 stable-aarch64-apple-darwin (默认) Python 3.82 (arm64)

这就是我如何查看 pyo3 的依赖项

[dependencies.pyo3]
version = "0.14.5"
features = ["extension-module"]
Run Code Online (Sandbox Code Playgroud)

什么对我有用

  1. 将 python 更改为 x86_64,并将 rust 工具链更改为 x86_64。这有效。

还有其他人在使用 Arm Mac 编译 Rust 时遇到问题吗?

这是我正在尝试的示例代码https://github.com/bedroombuilds/python2rust/tree/main/15_pymod_in_rust/rust/pyo3_monte_carlo_pi

Pat*_*Pat 6

我设法通过将其添加到 Cargo.toml 文件来解决它

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]
Run Code Online (Sandbox Code Playgroud)

PyO3 文档获取