Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking errors when a project contains a binary and a library #1084

Open
benruijl opened this issue Aug 6, 2020 · 2 comments · May be fixed by #2135
Open

Linking errors when a project contains a binary and a library #1084

benruijl opened this issue Aug 6, 2020 · 2 comments · May be fixed by #2135

Comments

@benruijl
Copy link

benruijl commented Aug 6, 2020

🐛 Bug Reports

Using the minimal setup below, which contains a main.rs and a lib.rs, I get the following linker error:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.2364k5u36xjw4mn8.rcgu.o" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.3wp0zm2gkkx6swv8.rcgu.o" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.46822g7yvqxpgt2s.rcgu.o" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.4zg4tmuhj52ks96w.rcgu.o" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.vkoh5x8xi1cexdw.rcgu.o" "-o" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef" "/home/ben/pythonapi/target/debug/deps/bar-1cd5931f960d78ef.1y7gc8l4weaej8o9.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/ben/pythonapi/target/debug/deps" "-L" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib" "/home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib" "/home/ben/pythonapi/target/debug/deps/libparking_lot-80ea636726af17a2.rlib" "/home/ben/pythonapi/target/debug/deps/libparking_lot_core-28df18bc562f242a.rlib" "/home/ben/pythonapi/target/debug/deps/libsmallvec-fb9d0a06a85a61af.rlib" "/home/ben/pythonapi/target/debug/deps/libcfg_if-ab1bd98be96b57b3.rlib" "/home/ben/pythonapi/target/debug/deps/liblock_api-670d43905d10a659.rlib" "/home/ben/pythonapi/target/debug/deps/libscopeguard-c8e12dbcd597c2e4.rlib" "/home/ben/pythonapi/target/debug/deps/libinstant-5a5c9b71ee271cc2.rlib" "/home/ben/pythonapi/target/debug/deps/liblibc-809b3ff95751964d.rlib" "/home/ben/pythonapi/target/debug/deps/libunindent-b569d19c5d9fcd61.rlib" "/home/ben/pythonapi/target/debug/deps/libpaste-8d6803f4aa113c0d.rlib" "/home/ben/pythonapi/target/debug/deps/libinventory-9412637833a73b14.rlib" "/home/ben/pythonapi/target/debug/deps/libindoc-734b491b84a0d49a.rlib" "-Wl,--start-group" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8bb11f807a7b6b4c.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-ca8087507780d964.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-259c92b387c1c166.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-16e0a2fbbb8e14b3.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace-8a1651b8e23d2aaf.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-23bdd98b0574083e.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-855a92055ec33e2e.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-24c70dd44fbacdfb.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-b326273841bae587.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-ac3c1f0e16507051.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-a5729542b65954aa.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-09bd1119ab1cad7d.rlib" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9a787681bfbeaf61.rlib" "-Wl,--end-group" "/home/ben/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-5a0398ee67f74664.rlib" "-Wl,-Bdynamic" "-lutil" "-ldl" "-lutil" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil"
  = note: /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib(bar-20b4232cfeb2e343.pwt7b75ceqh6rgt.rcgu.o): in function `pyo3::err::PyErr::restore':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:371: undefined reference to `PyErr_Restore'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib(bar-20b4232cfeb2e343.wjxnm2eusla6pko.rcgu.o): in function `pyo3::types::tuple::<impl pyo3::conversion::ToPyObject for (A,)>::to_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/tuple.rs:151: undefined reference to `PyTuple_New'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/tuple.rs:152: undefined reference to `PyTuple_SetItem'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib(bar-20b4232cfeb2e343.1b5kxnlpjv73af4c.rcgu.o): in function `pyo3::types::tuple::<impl pyo3::conversion::ToPyObject for (A,)>::to_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/tuple.rs:151: undefined reference to `PyTuple_New'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/tuple.rs:152: undefined reference to `PyTuple_SetItem'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib(bar-20b4232cfeb2e343.3kavbq3o9ru1tgyk.rcgu.o): in function `<pyo3::types::any::PyAny as pyo3::type_object::PyTypeInfo>::type_object_raw':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/mod.rs:147: undefined reference to `PyBaseObject_Type'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libbar-20b4232cfeb2e343.rlib(bar-20b4232cfeb2e343.3smrrh9783l6qj2r.rcgu.o): in function `pyo3::pyclass::default_new':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/pyclass.rs:(.text._ZN4pyo37pyclass11default_new17h53bf5ca33b236b33E+0xaa): undefined reference to `PyType_GenericAlloc'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.0.rcgu.o): in function `pyo3::err::PyErr::fetch':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:189: undefined reference to `PyErr_Fetch'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.0.rcgu.o): in function `pyo3::err::PyErr::new_type':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:236: undefined reference to `PyErr_NewException'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.0.rcgu.o): in function `pyo3::err::PyErr::print':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:275: undefined reference to `PyErr_PrintEx'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.0.rcgu.o): in function `pyo3::err::PyErr::restore':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:371: undefined reference to `PyErr_Restore'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.0.rcgu.o): in function `pyo3::err::panic_after_error':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/err.rs:578: undefined reference to `PyErr_Print'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.10.rcgu.o): in function `pyo3::types::string::PyString::new':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/string.rs:28: undefined reference to `PyUnicode_FromStringAndSize'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.10.rcgu.o): in function `pyo3::types::string::PyString::as_bytes':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/types/string.rs:50: undefined reference to `PyUnicode_AsUTF8AndSize'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.13.rcgu.o): in function `pyo3::ffi::object::Py_DECREF':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/ffi/object.rs:945: undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.13.rcgu.o): in function `pyo3::ffi::object::Py_None':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/ffi/object.rs:987: undefined reference to `_Py_NoneStruct'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.7.rcgu.o): in function `pyo3::gil::prepare_freethreaded_python::{{closure}}':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:84: undefined reference to `Py_IsInitialized'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:103: undefined reference to `Py_InitializeEx'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:126: undefined reference to `PyEval_SaveThread'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.7.rcgu.o): in function `pyo3::gil::prepare_freethreaded_python::{{closure}}::finalize':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:108: undefined reference to `Py_IsInitialized'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:109: undefined reference to `PyGILState_Ensure'
          /usr/bin/ld: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:110: undefined reference to `Py_Finalize'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.7.rcgu.o): in function `pyo3::gil::GILGuard::acquire':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:163: undefined reference to `PyGILState_Ensure'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.7.rcgu.o): in function `<pyo3::gil::GILGuard as core::ops::drop::Drop>::drop':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/gil.rs:193: undefined reference to `PyGILState_Release'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.9.rcgu.o): in function `<pyo3::exceptions::BaseException as pyo3::type_object::PyTypeObject>::type_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/exceptions.rs:223: undefined reference to `PyExc_BaseException'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.9.rcgu.o): in function `<pyo3::exceptions::SystemError as pyo3::type_object::PyTypeObject>::type_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/exceptions.rs:223: undefined reference to `PyExc_SystemError'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.9.rcgu.o): in function `<pyo3::exceptions::TypeError as pyo3::type_object::PyTypeObject>::type_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/exceptions.rs:223: undefined reference to `PyExc_TypeError'
          /usr/bin/ld: /home/ben/pythonapi/target/debug/deps/libpyo3-9f8b2bfa83998ca0.rlib(pyo3-9f8b2bfa83998ca0.pyo3.dfhfqc1n-cgu.9.rcgu.o): in function `<pyo3::exceptions::UnicodeDecodeError as pyo3::type_object::PyTypeObject>::type_object':
          /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/exceptions.rs:223: undefined reference to `PyExc_UnicodeDecodeError'
          collect2: error: ld returned 1 exit status
          

🌍 Environment

  • Your operating system and version: Arch Linux
  • Your python version: 3.8.5
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: pacman
  • Your Rust version (rustc --version): 1.45.2
  • Your PyO3 version: 0.11.1
  • Have you tried using latest PyO3 master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes

💥 Reproducing

cargo.toml

[package]
edition = "2018"
name = "bar"
version = "0.1.0"

[dependencies.pyo3]
features = ["extension-module"]
version = "0.11.1"

[lib]
crate-type = ["lib"]
name = "bar"

lib.rs:

use pyo3::prelude::*;

pub struct Foo {}

impl Foo {
    pub fn a() -> Foo {
        Foo {}
    }
}

#[pyclass]
struct SomePython {}

#[pymethods]
impl SomePython {
    #[new]
    fn new() -> SomePython {
        SomePython {}
    }
}

main.rs:

use bar::Foo;

fn main() {
    Foo::a();
}
@davidhewitt
Copy link
Member

Unfortunately this is our extension-module feature causing issues. Please see this FAQ entry which has a workaround for cargo test; you'll have to use this workaround when building the binary.

We're actively prototyping ways to make this API better to use. See e.g. #1040

@rookboom
Copy link

rookboom commented Jan 23, 2022

I get the same error, but managed to solve it with the workaround...

foo_lib

Cargo.toml

[package]
name = "foo_lib"
version = "0.1.0"
edition = "2021"

[dependencies]
pyo3 = "^0.15"

[features]
default = ["pyo3/extension-module"]

foo_bin

Cargo.toml

[package]
name = "foo_bin"
version = "0.1.0"
edition = "2021"


[dependencies]
foo_lib = {path = "../foo_lib", default-features = false}
pyo3 = "^0.15"

foo.zip

cargo run results in the same error above. cargo run --no-default-features works though.

@aviramha aviramha linked a pull request Jan 31, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants