PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Other
11.44k stars 693 forks source link

Compilation error on MacOS (both arm64 and x86_64) with Python 3.11.6 & Rust 1.79.0 installed via asdf #4279

Closed nileshtrivedi closed 1 week ago

nileshtrivedi commented 1 week ago

Bug Description

Undefined symbols for architecture x86_64:
 "_PyBaseObject_Type", referenced from:
...
ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `peano` (lib) due to 1 previous error

Trying to run cargo build in this project.

First I thought it was an Apple silicon issue, but it also fails on intel Mac.

Steps to Reproduce

My Python and Rust are installed via asdf

I try to build the rust library in this project.

I have tried all the tricks I could find:

Added this in 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",
]

Added this in build.rs:

fn main() {
  println!(
      "cargo:rustc-link-arg=-Wl,-rpath,/Library/Developer/CommandLineTools/Library/Frameworks"
  );
}

Backtrace

No response

Your operating system and version

Sonoma 14.5

Your Python version (python --version)

3.11.6

Your Rust version (rustc --version)

rustc 1.79.0 (129f3b996 2024-06-10)

Your PyO3 version

0.18.2

How did you install python? Did you use a virtualenv?

asdf Not using virtualenv.

Additional Info

$ which python
/Users/helix/.asdf/shims/python
$ PYO3_PRINT_CONFIG=1 cargo test
warning: unused manifest key: target.aarch64-apple-darwin.rustflags
warning: unused manifest key: target.x86_64-apple-darwin.rustflags
   Compiling pyo3-ffi v0.18.2
error: failed to run custom build command for `pyo3-ffi v0.18.2`

Caused by:
  process didn't exit successfully: `/Users/helix/code/peano/environment/target/debug/build/pyo3-ffi-4888c96affd219bc/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=PYO3_CROSS
  cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR
  cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION
  cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_IMPLEMENTATION
  cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG

  -- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --
  implementation=CPython
  version=3.11
  shared=true
  abi3=false
  lib_name=python3.11
  lib_dir=/Users/helix/.asdf/installs/python/3.11.6/lib
  executable=/Users/helix/.asdf/installs/python/3.11.6/bin/python
  pointer_width=64
  build_flags=
  suppress_build_script_link_lines=false

  note: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config
$ xcode-select -v
xcode-select version 2408.

Changing Pyo3 version to 0.20.2 in Cargo.toml did not help.

nileshtrivedi commented 1 week ago

Ah, I had been adding rustflags to Cargo.toml of the project by mistake. Adding this to ~/.cargo/config.toml (which had to be created) fixed it - at least on M1 / arm64.