PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.19k stars 752 forks source link

Linking problems when creating pyo3 wrapper around rust crate - which itself is a wrapper around a C library #3620

Closed joakim-hove closed 10 months ago

joakim-hove commented 10 months ago

Bug Description

I have a rust project consisting of two layered crates:

cmsis-dsp:This crate is based on an external C library. The rust crate is mainly just a wrapper around the cmake based build system of the external project.

dsp: This is a normal rust crate which depends on - and extends the cmsis-dsp crate.

Both the cmsis-dsp and dspcrates are compiled with #![no_std], whereas the python wrapper will be used on a normal PC with std support.

The combination of the cmsis-dsp and dsp crates have been used for several months and work nicely. I now want to create Python wrappers for the dsp crate using PyO3. I have created a new crate dsp-python with maturin new and added a minimal code dependency on the dsp project. When I try to build the new dsp-python project the cargo build step fails with a linking error.

The code is part of a quite large internal project - but if it is interesting I would certainly be happy to create a minimal project to illustrate the problem.

Steps to Reproduce

See bug description

Backtrace

Linker error:

bash$ cargo build
Compiling dsp-python v0.1.0 (/common/dsp-python)
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/hove/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/hove/.cargo/bin:/home/hove/.local/bin:/home/hove/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/hove/.dotnet/tools:/home
.....
ns-df2fb7f50dec519a.rlib" "-Wl,-Bdynamic" "-lCMSISDSP" "-lpython3.10" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/hove/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/hove/work/sonair/sonair/firmware/rust/common/dsp-python/target/debug/deps/libdsp_python.so" "-Wl,--gc-sections" "-shared" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: /usr/bin/ld:/tmp/rustcSOWcFj/list:4: syntax error in VERSION script
          collect2: error: ld returned 1 exit status


### Your operating system and version

Linux / ubuntu

### Your Python version (`python --version`)

Python 3.10

### Your Rust version (`rustc --version`)

1.73

### Your PyO3 version

maturin: 1.1.0

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

I used a virtual env generated with `python -m venv /path/to/venv`

### Additional Info

_No response_
davidhewitt commented 10 months ago

Nothing obvious springs to mind. Can you share a small repro with instructions to get a reproduction of the issue?

joakim-hove commented 10 months ago

Nothing obvious springs to mind. Can you share a small repro with instructions to get a reproduction of the issue?

Very good - I will create a small project illustrating the issue; might have to wait until tomorrow though.

davidhewitt commented 10 months ago

No rush, it may take me some time to investigate 👍

joakim-hove commented 10 months ago

Thank you for willingness to look into this - that turned out to more-or-less solve the problem. When I made a minimal example to illustrate the problem that actually worked as it should, so now I am well set up to debug this properly.