From 1ebceb4f86423b1f15fc8ffbacac2a17599e3dcf Mon Sep 17 00:00:00 2001 From: Nathan Henrie Date: Tue, 2 Aug 2022 16:52:53 -0600 Subject: [PATCH] Additional link args and troubleshooting for MacOS Fixes https://github.com/PyO3/pyo3/issues/1800 Documents issues linking with the system python (`/usr/bin/python3`) as well as reemphasizes the need for `--no-default-features` with `cargo test` on MacOS --- guide/src/building_and_distribution.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/guide/src/building_and_distribution.md b/guide/src/building_and_distribution.md index 796c5d4e..d123842d 100644 --- a/guide/src/building_and_distribution.md +++ b/guide/src/building_and_distribution.md @@ -135,6 +135,29 @@ rustflags = [ ] ``` +Using the MacOS system python3 (`/usr/bin/python3`, as opposed to python installed via homebrew, pyenv, nix, etc.) may result in runtime errors such as `Library not loaded: @rpath/Python3.framework/Versions/3.8/Python3`. These can be resolved with another addition to `.cargo/config.toml`: + +```toml +[build] +rustflags = [ + "-C", "link-args=-Wl,-rpath,/Library/Developer/CommandLineTools/Library/Frameworks", +] +``` + +Alternatively, on rust >= 1.56, one can include in `build.rs`: + +```rust +fn main() { + println!( + "cargo:rustc-link-arg=-Wl,-rpath,/Library/Developer/CommandLineTools/Library/Frameworks" + ); +} +``` + +For more discussion on and workarounds for MacOS linking problems [see this issue](https://github.com/PyO3/pyo3/issues/1800#issuecomment-906786649). + +Finally, don't forget that on MacOS the `extension-module` feature will cause `cargo test` to fail without the `--no-default-features` flag (see [the FAQ](https://pyo3.rs/main/faq.html#i-cant-run-cargo-test-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror)). + ### The `extension-module` feature PyO3's `extension-module` feature is used to disable [linking](https://en.wikipedia.org/wiki/Linker_(computing)) to `libpython` on unix targets.