Skip to content

Linker errors with #[gen_stub_pyfunction] #161

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

Open
chingiztob opened this issue Mar 29, 2025 · 18 comments
Open

Linker errors with #[gen_stub_pyfunction] #161

chingiztob opened this issue Mar 29, 2025 · 18 comments
Labels
question Further information is requested

Comments

@chingiztob
Copy link

I get compile errors if the derive macro #[gen_stub_pyfunction] is placed above #[pyfunction(...)] or #[pyo3(...)]. When placed under #[pyfunction(...)], the error does not occur, but the .pyi signature is incorrect, as it ignores the signature and name attributes (i.e., #[pyfunction(name = "foo_bar")]).

PyO3: 0.24
PyO3-stub-gen: 0.7
CC: 1.2.17

error: linking with `cc` failed: exit status: 1
  = note: LC_ALL="C" PATH="/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/user/.vscode/extensions/ms-python.python-2025.2.0-linux-x64/python_files/deactivate/fish:/home/user/Rust/py_rust/my_crate/.venv/bin:/home/user/.vscode/extensions/ms-python.python-2025.2.0-linux-x64/python_files/deactivate/fish:/home/user/Rust/py_rust/my_crate/.venv/bin:/home/user/.vscode/extensions/ms-python.python-2025.2.0-linux-x64/python_files/deactivate/fish:/home/user/Rust/py_rust/my_crate/.venv/bin:/home/user/.cargo/bin:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcTIykf5/symbols.o" "<7 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/user/Rust/py_rust/my_crate/target/debug/deps/{libmy_crate.rlib,libpyo3_log-9b03dc8c4aefc17e.rlib,libarc_swap-10e0d45470cef73f.rlib,libserde_json-cf7c37afc50a5deb.rlib,libwkt-02b5fef8c4546365.rlib,libgeo_traits-19a9fbc0664de443.rlib,libmy_crate_core-8fc4e5b22734d954.rlib,librustworkx_core-f46641ff7eff3fcd.rlib,librand_pcg-1d4e8a5a962be962.rlib,librand-3bd19b143df47d6d.rlib,librand_chacha-0ec573e0071e21d8.rlib,libppv_lite86-60e05cf8641d4eb3.rlib,libzerocopy-af6f78c3709746d0.rlib,librand_core-71702c6935d88a7e.rlib,libpriority_queue-6d2fcb4ba0d64a90.rlib,librayon_cond-cbd0b2666e046844.rlib,libosm4routing-3c0d4661bcf80bf5.rlib,libcsv-c181b437a1a14dd6.rlib,libryu-60c6d50bed860378.rlib,libitoa-168a61ec00c46309.rlib,libcsv_core-fdeaf956340de454.rlib,libmemchr-52aca85a6fe70283.rlib,libosmpbfreader-ee3e95a4b9a3f5a7.rlib,libflat_map-f9e23b642e9a2dad.rlib,libself_cell-6c5c13bac1c8bf27.rlib,libflate2-470e93d426fa42ce.rlib,libminiz_oxide-3e8a89d1dd09d4ca.rlib,libadler2-f33a6e9350b11aeb.rlib,libcrc32fast-6babef480be3e227.rlib,libprotobuf-dc02f8b4ee55f5af.rlib,libprotobuf_support-5216950db4401d4d.rlib,libthiserror-064f81e58f1fca52.rlib,libpar_map-6bd421a8ba4d1cce.rlib,libpub_iterator_type-8c376af37b08eb25.rlib,libfutures_cpupool-5ea901caf7635438.rlib,libnum_cpus-0dcf56364f957cb0.rlib,libfutures-3e31a415cdb786ba.rlib,libsmartstring-805cc0a7305979cd.rlib,libstatic_assertions-46d4eaa74ce7e710.rlib,libh3o-674033b8a6f10fc8.rlib,libh3o_bit-e9ffc78fe8e15eb0.rlib,libahash-cc76b1c3df111a40.rlib,libgetrandom-8e215e35e5a244e2.rlib,libzerocopy-60eaf84cc6fdd078.rlib,libfloat_eq-0895dfbd108cd5c2.rlib,libgeo-5a57b79eac42fbd0.rlib,libfloat_next_after-b2ebc87021339837.rlib,libearcutr-fd3305546a4c73be.rlib,libitertools-6718df69b18c2afc.rlib,libspade-d4d67023171a4ceb.rlib,libgeographiclib_rs-d7810737768bf6bb.rlib,liblibm-8a4b8a637c241f19.rlib,libi_overlay-d659fe7fb53129d8.rlib,libi_key_sort-023b65f99d2759a8.rlib,libi_shape-dcf14e161f98aa78.rlib,libi_tree-0eeba0dc7d972bfb.rlib,libi_float-11e58232759dac25.rlib,librobust-c878b2d325ca55d8.rlib,libgeo_types-43d112f21ff61e5d.rlib,librstar-49cb74b63460d94a.rlib,libheapless-eee61aecd39d84ed.rlib,libhash32-99d9019d851ae6aa.rlib,libbyteorder-cd68bf5e00b92e30.rlib,libsmallvec-b6415380449fa252.rlib,libapprox-ea70188c3dff4f0c.rlib,libthiserror-f42dd635b285d638.rlib,libpetgraph-c9eb23c0ca1639e8.rlib,libfixedbitset-aee977be4f2eb436.rlib,libpyo3_stub_gen-1fdd038974e6c628.rlib,libtoml-321947d36aa481aa.rlib,libtoml_edit-449338eb6a685a41.rlib,libserde_spanned-380db2fede07922e.rlib,libindexmap-a9da3c33a21bcac0.rlib,libhashbrown-02ebcabe0c8de4cb.rlib,libfoldhash-27d05bb4c9ea14de.rlib,libequivalent-092aef85020331e6.rlib,liballocator_api2-e0c4e248aa5b247c.rlib,libwinnow-4b0780e43845a88a.rlib,libtoml_datetime-f3715e2f6b6e1f86.rlib,liblog-b67c43f442cc8df1.rlib,libnumpy-6cfb5afb4daed64a.rlib,librustc_hash-2a18977b8051a3c8.rlib,libndarray-e4b11ed56a23d23e.rlib,libmatrixmultiply-dea3ee4653f3e365.rlib,libnum_complex-d620c9facc6b15be.rlib,librayon-8e6b2a1f23751866.rlib,librayon_core-5c1449d9dee3b313.rlib,libcrossbeam_deque-b329f1f0050a15af.rlib,libcrossbeam_epoch-a3374cd34dff5517.rlib,libcrossbeam_utils-16972a7f859230f4.rlib,libnum_integer-e29ad87393439852.rlib,librawpointer-5d6171626f3688e3.rlib,libmaplit-f5f9584d5d9f8f71.rlib,libchrono-5faa7d2117fd956d.rlib,libnum_traits-25926751157e6588.rlib,libiana_time_zone-df599653c0718876.rlib,libserde-cb480e8971244239.rlib,libanyhow-60d404d1399bd7aa.rlib,libitertools-ef5d8353680c0c2a.rlib,libeither-52abeaa667b2dc4b.rlib,libpyo3-4c3aa99863614279.rlib,libcfg_if-574e11b886f995a8.rlib,libmemoffset-67b9cb995d8f5827.rlib,libonce_cell-7a7cb004a21232db.rlib,libpyo3_ffi-0baad43f0903f2e1.rlib,liblibc-4f4b67a75c952142.rlib,libunindent-5cb612681f0f1f01.rlib,libinventory-4e6f654963eab0f9.rlib}" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-1e40119a663b1e41.rlib,libpanic_unwind-c17245bd091354df.rlib,libobject-cd96b354c08fae15.rlib,libmemchr-65e636254baf89ab.rlib,libaddr2line-10dcc9a2e78b2b1e.rlib,libgimli-73aed197564ca365.rlib,librustc_demangle-6d92069f05504d6a.rlib,libstd_detect-3ddba380cdb712f3.rlib,libhashbrown-1c9b748326f090af.rlib,librustc_std_workspace_alloc-61109fcaa2782b03.rlib,libminiz_oxide-4f430c1ad3399055.rlib,libadler-396e0f8ad43d4281.rlib,libunwind-b3ec995576ac8387.rlib,libcfg_if-7a8d556bd2496467.rlib,liblibc-2cd05d129739cd79.rlib,liballoc-dcbc78300c47b651.rlib,librustc_std_workspace_core-3d3fdd80ec3c369e.rlib,libcore-919178916a309d3e.rlib,libcompiler_builtins-6497188c539eb018.rlib}" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/user/Rust/py_rust/my_crate/target/debug/deps/stub_gen-28bc6f2ddbd941c4" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3_stub_gen-1fdd038974e6c628.rlib(pyo3_stub_gen-1fdd038974e6c628.pyo3_stub_gen.bb6e24692b012746-cgu.09.rcgu.o): in function `pyo3_ffi::object::PyObject_TypeCheck':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:382:(.text._ZN8pyo3_ffi6object18PyObject_TypeCheck17h5047a33bff810595E+0x34): undefined reference to `PyType_IsSubtype'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3_stub_gen-1fdd038974e6c628.rlib(pyo3_stub_gen-1fdd038974e6c628.pyo3_stub_gen.bb6e24692b012746-cgu.09.rcgu.o): in function `pyo3_ffi::object::Py_None':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:855:(.text._ZN8pyo3_ffi6object7Py_None17h9afe8e04802c6e9cE+0x3): undefined reference to `_Py_NoneStruct'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3_stub_gen-1fdd038974e6c628.rlib(pyo3_stub_gen-1fdd038974e6c628.pyo3_stub_gen.bb6e24692b012746-cgu.10.rcgu.o): in function `pyo3::types::dict::DictIterImpl::next_unchecked':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/dict.rs:484:(.text._ZN4pyo35types4dict12DictIterImpl14next_unchecked17h2b74fc091f84bfbeE+0x13a): undefined reference to `PyDict_Next'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libnumpy-6cfb5afb4daed64a.rlib(numpy-6cfb5afb4daed64a.numpy.b533e71f2611061e-cgu.2.rcgu.o): in function `<pyo3::types::tuple::PyTuple as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN77_$LT$pyo3..types..tuple..PyTuple$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h1804a61c3582b1dbE+0x8): undefined reference to `PyTuple_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libnumpy-6cfb5afb4daed64a.rlib(numpy-6cfb5afb4daed64a.numpy.b533e71f2611061e-cgu.2.rcgu.o): in function `pyo3_ffi::object::PyObject_TypeCheck':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:382:(.text._ZN8pyo3_ffi6object18PyObject_TypeCheck17h8789fe1daf4cef86E+0x34): undefined reference to `PyType_IsSubtype'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `pyo3_ffi::object::Py_DECREF':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN72_$LT$pyo3..instance..Bound$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h04ca0ed1fd03c297E+0x11e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN72_$LT$pyo3..instance..Bound$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h2ad6071192c27aedE+0x11e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN72_$LT$pyo3..instance..Bound$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h2f0bc1469a672821E+0x11e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN72_$LT$pyo3..instance..Bound$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h4ced8bb18d5fe4deE+0x11e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN72_$LT$pyo3..instance..Bound$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h6996a46ef2b836efE+0x11e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o):/home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739: more undefined references to `_Py_Dealloc' follow
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::any::PyAny> as pyo3::types::any::PyAnyMethods>::call0':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/any.rs:1228:(.text._ZN103_$LT$pyo3..instance..Bound$LT$pyo3..types..any..PyAny$GT$$u20$as$u20$pyo3..types..any..PyAnyMethods$GT$5call017h4743d15f27e61299E+0x29): undefined reference to `PyObject_CallNoArgs'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::any::PyAny> as pyo3::types::any::PyAnyMethods>::repr':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/any.rs:1454:(.text._ZN103_$LT$pyo3..instance..Bound$LT$pyo3..types..any..PyAny$GT$$u20$as$u20$pyo3..types..any..PyAnyMethods$GT$4repr17h7be362d6147e5093E+0x2b): undefined reference to `PyObject_Repr'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::any::PyAny> as pyo3::types::any::PyAnyMethods>::str':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/any.rs:1462:(.text._ZN103_$LT$pyo3..instance..Bound$LT$pyo3..types..any..PyAny$GT$$u20$as$u20$pyo3..types..any..PyAnyMethods$GT$3str17h37c59d4f069c3edbE+0x2b): undefined reference to `PyObject_Str'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `pyo3::types::bytes::<impl pyo3::instance::Borrowed<pyo3::types::bytes::PyBytes>>::as_bytes':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/bytes.rs:181:(.text._ZN4pyo35types5bytes77_$LT$impl$u20$pyo3..instance..Borrowed$LT$pyo3..types..bytes..PyBytes$GT$$GT$8as_bytes17hee927166c0c47f61E+0x19): undefined reference to `PyBytes_AsString'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/bytes.rs:182:(.text._ZN4pyo35types5bytes77_$LT$impl$u20$pyo3..instance..Borrowed$LT$pyo3..types..bytes..PyBytes$GT$$GT$8as_bytes17hee927166c0c47f61E+0x37): undefined reference to `PyBytes_Size'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `pyo3::types::string::<impl pyo3::instance::Borrowed<pyo3::types::string::PyString>>::to_str':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/string.rs:324:(.text._ZN4pyo35types6string79_$LT$impl$u20$pyo3..instance..Borrowed$LT$pyo3..types..string..PyString$GT$$GT$6to_str17he1033e995819a48bE+0x30): undefined reference to `PyUnicode_AsUTF8AndSize'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `pyo3::types::string::<impl pyo3::instance::Borrowed<pyo3::types::string::PyString>>::to_string_lossy':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/string.rs:363:(.text._ZN4pyo35types6string79_$LT$impl$u20$pyo3..instance..Borrowed$LT$pyo3..types..string..PyString$GT$$GT$15to_string_lossy17hb37d78ac190a7bf5E+0x107): undefined reference to `PyUnicode_AsEncodedString'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::traceback::PyTraceback> as pyo3::types::traceback::PyTracebackMethods>::format':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/traceback.rs:69:(.text._ZN127_$LT$pyo3..instance..Bound$LT$pyo3..types..traceback..PyTraceback$GT$$u20$as$u20$pyo3..types..traceback..PyTracebackMethods$GT$6format17h7dc568c6f828fef5E+0x330): undefined reference to `PyTraceBack_Print'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::typeobject::PyType> as pyo3::types::typeobject::PyTypeMethods>::name':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/typeobject.rs:119:(.text._ZN119_$LT$pyo3..instance..Bound$LT$pyo3..types..typeobject..PyType$GT$$u20$as$u20$pyo3..types..typeobject..PyTypeMethods$GT$4name17h7000a45cde7a644dE+0x2f): undefined reference to `PyType_GetName'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.00.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::typeobject::PyType> as pyo3::types::typeobject::PyTypeMethods>::qualname':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/typeobject.rs:138:(.text._ZN119_$LT$pyo3..instance..Bound$LT$pyo3..types..typeobject..PyType$GT$$u20$as$u20$pyo3..types..typeobject..PyTypeMethods$GT$8qualname17h9cecfdc2a43fba30E+0x2f): undefined reference to `PyType_GetQualName'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.04.rcgu.o): in function `pyo3::types::module::PyModule::import':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/module.rs:96:(.text._ZN4pyo35types6module8PyModule6import17h9d7bc4ca1fda3958E+0xc3): undefined reference to `PyImport_Import'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.05.rcgu.o): in function `pyo3::conversions::std::num::<impl pyo3::conversion::IntoPyObject for u32>::into_pyobject':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/conversions/std/num.rs:191:(.text._ZN4pyo311conversions3std3num64_$LT$impl$u20$pyo3..conversion..IntoPyObject$u20$for$u20$u32$GT$13into_pyobject17h4251268fe478fbbbE+0xe): undefined reference to `PyLong_FromLong'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `pyo3::gil::prepare_freethreaded_python::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:68:(.text._ZN4pyo33gil27prepare_freethreaded_python28_$u7b$$u7b$closure$u7d$$u7d$17ha08ce94e6156acceE+0xb): undefined reference to `Py_IsInitialized'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:69:(.text._ZN4pyo33gil27prepare_freethreaded_python28_$u7b$$u7b$closure$u7d$$u7d$17ha08ce94e6156acceE+0x18): undefined reference to `Py_InitializeEx'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:72:(.text._ZN4pyo33gil27prepare_freethreaded_python28_$u7b$$u7b$closure$u7d$$u7d$17ha08ce94e6156acceE+0x1e): undefined reference to `PyEval_SaveThread'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `<pyo3::gil::GILGuard as core::ops::drop::Drop>::drop':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:242:(.text._ZN61_$LT$pyo3..gil..GILGuard$u20$as$u20$core..ops..drop..Drop$GT$4drop17h3ac2982827f956daE+0x40): undefined reference to `PyGILState_Release'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `pyo3_ffi::object::Py_DECREF':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN4pyo33gil13ReferencePool13update_counts17hdbcd53dd200c5c12E+0x42e): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `pyo3::gil::SuspendGIL::new':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:303:(.text._ZN4pyo33gil10SuspendGIL3new17h5c3bef63f412a267E+0x20): undefined reference to `PyEval_SaveThread'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `<pyo3::gil::SuspendGIL as core::ops::drop::Drop>::drop':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:313:(.text._ZN63_$LT$pyo3..gil..SuspendGIL$u20$as$u20$core..ops..drop..Drop$GT$4drop17heac507345a0dd964E+0x29): undefined reference to `PyEval_RestoreThread'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `pyo3_ffi::object::Py_DECREF':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:739:(.text._ZN4pyo33gil15register_decref17h85f92955ccea50e1E+0x15f): undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `pyo3::gil::GILGuard::acquire::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/gil.rs:182:(.text._ZN4pyo33gil8GILGuard7acquire28_$u7b$$u7b$closure$u7d$$u7d$17hf8e66e70b00b2ccbE+0x11): undefined reference to `Py_IsInitialized'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.07.rcgu.o): in function `<pyo3::types::list::PyList as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN75_$LT$pyo3..types..list..PyList$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h9009a43a90160631E+0x8): undefined reference to `PyList_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::err::err_state::PyErrStateNormalized::ptraceback':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/err_state.rs:179:(.text._ZN4pyo33err9err_state20PyErrStateNormalized10ptraceback17hdc90cb0c8e98da9bE+0x14): undefined reference to `PyException_GetTraceback'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::err::err_state::PyErrStateNormalized::take':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/err_state.rs:190:(.text._ZN4pyo33err9err_state20PyErrStateNormalized4take17h7b331e6da1ed2298E+0x3): undefined reference to `PyErr_GetRaisedException'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::err::err_state::PyErrStateInner::restore':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/err_state.rs:320:(.text._ZN4pyo33err9err_state15PyErrStateInner7restore17h8f5a02db1d4e57a8E+0x54): undefined reference to `PyErr_SetRaisedException'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::err::err_state::raise_lazy':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/err_state.rs:355:(.text._ZN4pyo33err9err_state10raise_lazy17h1c99f9bdaabb9fceE+0xe6): undefined reference to `PyErr_SetString'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/err_state.rs:360:(.text._ZN4pyo33err9err_state10raise_lazy17h1c99f9bdaabb9fceE+0x119): undefined reference to `PyErr_SetObject'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::types::string::PyString::new':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/string.rs:171:(.text._ZN4pyo35types6string8PyString3new17h247ddbc034c203f4E+0x3b): undefined reference to `PyUnicode_FromStringAndSize'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `pyo3::types::string::PyString::intern':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/string.rs:196:(.text._ZN4pyo35types6string8PyString6intern17hfa9a6434b584dc98E+0x39): undefined reference to `PyUnicode_FromStringAndSize'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/string.rs:198:(.text._ZN4pyo35types6string8PyString6intern17hfa9a6434b584dc98E+0x58): undefined reference to `PyUnicode_InternInPlace'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `<pyo3::exceptions::PyBaseException as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/exceptions.rs:282:(.text._ZN83_$LT$pyo3..exceptions..PyBaseException$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h8da994eb8f8197b8E+0x8): undefined reference to `PyExc_BaseException'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `<pyo3::exceptions::PyTypeError as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/exceptions.rs:282:(.text._ZN79_$LT$pyo3..exceptions..PyTypeError$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h1d380aa8be824173E+0x8): undefined reference to `PyExc_TypeError'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.08.rcgu.o): in function `<pyo3::types::string::PyString as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN79_$LT$pyo3..types..string..PyString$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h45186fb39d7351ccE+0x8): undefined reference to `PyUnicode_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.11.rcgu.o): in function `pyo3_ffi::object::PyObject_TypeCheck':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:382:(.text._ZN8pyo3_ffi6object18PyObject_TypeCheck17he076cf021ecd7e92E+0x34): undefined reference to `PyType_IsSubtype'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.11.rcgu.o): in function `pyo3_ffi::object::Py_SIZE':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:207:(.text._ZN8pyo3_ffi6object7Py_SIZE17hfdb13675f1e810a7E+0x23): undefined reference to `PyLong_Type'
          /usr/bin/ld: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/object.rs:208:(.text._ZN8pyo3_ffi6object7Py_SIZE17hfdb13675f1e810a7E+0x5f): undefined reference to `PyBool_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.12.rcgu.o): in function `<pyo3::instance::Bound<pyo3::types::any::PyAny> as pyo3::types::any::PyAnyMethods>::getattr::inner':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/any.rs:964:(.text._ZN103_$LT$pyo3..instance..Bound$LT$pyo3..types..any..PyAny$GT$$u20$as$u20$pyo3..types..any..PyAnyMethods$GT$7getattr5inner17hff27493b7d1d859cE+0x4a): undefined reference to `PyObject_GetAttr'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.13.rcgu.o): in function `<pyo3::types::boolobject::PyBool as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN81_$LT$pyo3..types..boolobject..PyBool$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17hd2f0f3d7f66619c6E+0x8): undefined reference to `PyBool_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.13.rcgu.o): in function `<pyo3::types::float::PyFloat as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN77_$LT$pyo3..types..float..PyFloat$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h2d71d799bf50e59bE+0x8): undefined reference to `PyFloat_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.14.rcgu.o): in function `pyo3::err::PyErr::print_panic_and_unwind':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/mod.rs:385:(.text._ZN4pyo33err5PyErr22print_panic_and_unwind17h35bd4e432b993fb0E+0x102): undefined reference to `PyErr_PrintEx'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.14.rcgu.o): in function `pyo3::err::PyErr::new_type':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/mod.rs:447:(.text._ZN4pyo33err5PyErr8new_type17h70cbe74275019b16E+0x222): undefined reference to `PyErr_NewExceptionWithDoc'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.14.rcgu.o): in function `pyo3::err::PyErr::write_unraisable':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/mod.rs:586:(.text._ZN4pyo33err5PyErr16write_unraisable17hdc3922c5be416cd6E+0x27): undefined reference to `PyErr_WriteUnraisable'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.14.rcgu.o): in function `pyo3::err::panic_after_error':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/err/mod.rs:953:(.text._ZN4pyo33err17panic_after_error17h5310a8917ebbb866E+0xc): undefined reference to `PyErr_Print'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.14.rcgu.o): in function `<pyo3::types::dict::PyDict as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN75_$LT$pyo3..types..dict..PyDict$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17hc1b55fffd302019bE+0x8): undefined reference to `PyDict_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3-4c3aa99863614279.rlib(pyo3-4c3aa99863614279.pyo3.92baa1a42c2db885-cgu.15.rcgu.o): in function `<pyo3::types::num::PyInt as pyo3::type_object::PyTypeInfo>::type_object_raw::{{closure}}':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.0/src/types/mod.rs:135:(.text._ZN73_$LT$pyo3..types..num..PyInt$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$15type_object_raw28_$u7b$$u7b$closure$u7d$$u7d$17h16d1fdf660b21323E+0x8): undefined reference to `PyLong_Type'
          /usr/bin/ld: /home/user/Rust/py_rust/my_crate/target/debug/deps/libpyo3_ffi-0baad43f0903f2e1.rlib(pyo3_ffi-0baad43f0903f2e1.pyo3_ffi.43cd707add4e6775-cgu.0.rcgu.o): in function `pyo3_ffi::pystate::PyGILState_Ensure':
          /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-ffi-0.24.0/src/pystate.rs:141:(.text._ZN8pyo3_ffi7pystate17PyGILState_Ensure17h4ab09a0c64be18f9E+0x11): undefined reference to `PyGILState_Ensure'
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)
@attila-lin
Copy link

same issue

@KristianApostolov
Copy link

KristianApostolov commented Apr 3, 2025

@chingiztob Did you try following Building and Distribution?
Also, could you share your Cargo.toml & stub example setup?

@chingiztob
Copy link
Author

chingiztob commented Apr 3, 2025

@chingiztob Did you try following Building an0d Distribution?
Also, could you share your Cargo.toml & stub example setup?

@KristianApostolov

Which exact part of "Building and Distribution" should I check? This section is huge, and I have no build issues with the Maturin/PyO3 project itself—only with stub_gen, which is compiled separately.

I will try to create a minimal reproducible example soon.

@KristianApostolov
Copy link

KristianApostolov commented Apr 3, 2025

@chingiztob Excuse me for not providing the correct section, updated the link in the original reply.

This might help as well:
I can't run cargo test; or I can't build in a Cargo workspace: I'm having linker issues like "Symbol not found" or "Undefined reference to _PyExc_SystemError"!

@chingiztob
Copy link
Author

@KristianApostolov

Unfortunately, none of this helps. I probably should have mentioned that the errors are occurring on a Linux machine, not a Mac.

As for a reproducible example, it turns out it's not so straightforward—it works on a basic "sum_as_string" project. I'll dig further.

use pyo3::prelude::*;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::gen_stub_pyfunction};

/// Formats the sum of two numbers as string.
#[gen_stub_pyfunction]
#[pyfunction]
#[pyo3(name = "bar", signature = (a,b = 2))]
pub fn sum_as_string( py: Python<'_>, a: usize, b: usize) -> PyResult<String> {
    py.allow_threads(|| {
        Ok((a + b).to_string())
    })
}

/// A Python module implemented in Rust.
#[pymodule]
fn error_test(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    Ok(())
}

define_stub_info_gatherer!(stub_info);

@termoshtt
Copy link
Member

undefined reference to `PyLong_Type'

This should be because pyo3's extension-modules feature is enabled. So you should

@termoshtt termoshtt added the question Further information is requested label Apr 5, 2025
@chingiztob
Copy link
Author

chingiztob commented Apr 5, 2025

undefined reference to `PyLong_Type'

This should be because pyo3's extension-modules feature is enabled. So you should

* Do not enable `pyo3/extension-modules` on `Cargo.toml`

* Enable it on `pyproject.toml` via `tool.maturin.features` like https://github.com/Jij-Inc/pyo3-stub-gen/blob/main/examples/mixed/pyproject.toml#L15

Okay, the linker errors are gone now, but there's a new error:

target/release/stub_gen: error while loading shared libraries: libpython3.13.so.1.0: cannot open shared object file: No such file or directory

Could this be caused by a difference between the Python version in the Venv (3.13.1) and the system Python version (3.12.3)?

@termoshtt
Copy link
Member

Could this be caused by a difference between the Python version in the Venv (3.13.1) and the system Python version (3.12.3)?

First, cargo clean may resolve this issue. Please try it.
Otherwise, the metadata of installed Python can be invalid.

PyO3 asks where libpython3.X.so exists to the python interpreter. You can get the answer by

python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))'

This returns the directory where libpython3.X.so exists, but some Python distribution does not correctly set it, e.g. python in Xcode or installed via uv before astral-sh/uv#8429 has been merged.

@chingiztob
Copy link
Author

This returns the directory where libpython3.X.so exists, but some Python distribution does not correctly set it, e.g. python in Xcode or installed via uv before astral-sh/uv#8429 has been merged.

Reinstalling the UV-managed Python version helped—thank you very much!

Also, it seems to behave differently (for some reason) when running cargo run --bin stub_gen from the VS Code terminal versus the system terminal, even when cd in the same folder.

@chingiztob
Copy link
Author

Well, now the GitHub Maturin action is broken for PyPy. Should I open a separate issue?

   Compiling pyo3-build-config v0.24.1
   Compiling pyo3-macros-backend v0.24.1
   Compiling pyo3-ffi v0.24.1
   Compiling pyo3 v0.24.1
   Compiling numpy v0.24.0
   Compiling pyo3-macros v0.24.1
   Compiling pyo3-log v0.12.3
   Compiling pyo3-stub-gen v0.7.0
   Compiling ferrobus v0.1.2 (/home/runner/work/ferrobus/ferrobus)
error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
   --> src/model.rs:99:1
    |
99  | #[gen_stub_pyfunction]
    | ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
    |
note: found an item that was configured out
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
    |
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
    |
345 | #[cfg(not(any(PyPy, GraalPy)))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
   --> src/routing.rs:126:1
    |
126 | #[gen_stub_pyfunction]
    | ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
    |
note: found an item that was configured out
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
    |
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
    |
345 | #[cfg(not(any(PyPy, GraalPy)))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
   --> src/routing.rs:199:1
    |
199 | #[gen_stub_pyfunction]
    | ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
    |
note: found an item that was configured out
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
    |
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
    |
345 | #[cfg(not(any(PyPy, GraalPy)))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
   --> src/routing.rs:258:1
    |
258 | #[gen_stub_pyfunction]
    | ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
    |
note: found an item that was configured out
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
    |
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
   --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
    |
345 | #[cfg(not(any(PyPy, GraalPy)))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0425`.
error: could not compile `ferrobus` (lib) due to 4 previous errors
💥 maturin failed
  Caused by: Failed to build a native library through cargo
  Caused by: Cargo build finished with "exit status: 101": `env -u CARGO CARGO_ENCODED_RUSTFLAGS="-C\u{1f}target-feature=-crt-static" PYO3_CONFIG_FILE="/home/runner/work/ferrobus/ferrobus/target/maturin/pyo3-config-aarch64-unknown-linux-musl-3.9.txt" "cargo" "rustc" "--features" "pyo3/extension-module" "--target" "aarch64-unknown-linux-musl" "--message-format" "json-render-diagnostics" "--manifest-path" "/home/runner/work/ferrobus/ferrobus/Cargo.toml" "--release" "--lib" "--crate-type" "cdylib"`
Error: The process '/usr/bin/docker' failed with exit code 1
    at ExecState._setResult (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1702:25)
Error: The process '/usr/bin/docker' failed with exit code 1
    at ExecState.CheckComplete (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1685:18)
    at ChildProcess.<anonymous> (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1579:27)
    at ChildProcess.emit (node:events:524:28)
    at maybeClose (node:internal/child_process:1104:16)
    at ChildProcess._handle.onexit (node:internal/child_process:304:5)

@Kile
Copy link

Kile commented Apr 12, 2025

Hi I am having the same issue (for GitHub Actions). Windows and MacOS compile fine, but all linux versions fail.

Image

I am passing --features "extension-module" to the Maturin action, but I am getting the same error as you just above

@chingiztob
Copy link
Author

@Kile Since stubgen is not necessary for maturin wheel builds. I fixed this by hiding all stubgen-related stuff behind a feature gate with a macro

use proc_macro::TokenStream;
use quote::quote;
use syn::{Item, parse_macro_input};

#[proc_macro_attribute]
pub fn stubgen(_attrs: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as Item);

    // Wrap the item with #[cfg_attr(feature = "stubgen", ...)]
    let output = match input {
        Item::Struct(s) => {
            quote! {
                #[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pyclass)]
                #s
            }
        }
        Item::Impl(i) => {
            quote! {
                #[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pymethods)]
                #i
            }
        }
        Item::Fn(f) => {
            quote! {
                #[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
                #f
            }
        }
        _ => {
            quote! {
                #input
            }
        }
    };

    output.into()
}

@Kile
Copy link

Kile commented Apr 13, 2025

Oh awesome thanks for the workaround! In which file does this go? Also would still be nice to have an official patch 😅

@chingiztob
Copy link
Author

chingiztob commented Apr 13, 2025

@Kile You would need to create a new lib crate in the workspace, place this macro there, replace all #[gen ...] with this macro, and also gate the define_stub_info_gatherer!(stub_info); in lib.rs.

Then make pyo3-stub-gen an optional dependency, and modify stub_gen.rs as well:

#[cfg(feature = "stubgen")]
use pyo3_stub_gen::Result;
#[cfg(feature = "stubgen")]
fn main() -> Result<()> {
    let stub = you_crate::stub_info()?;
    stub.generate()?;
    Ok(())
}

#[cfg(not(feature = "stubgen"))]
fn main() {
    eprintln!("The 'stubgen' feature is not enabled. Enable it with `--features stubgen`.");
}

This is kinda cringe solution, but it will work untill patch

p.s.: How to Write a Custom derive Macro

@Kile
Copy link

Kile commented Apr 13, 2025

@chingiztob thanks, wow that really is cringe. Any way to keep it in the same repo? Or do I need to completely restructure my repo to contain two crates?

@chingiztob
Copy link
Author

@Kile If you are not using workspace - something like this:

my_crate/
├── Cargo.toml          
├── src/
│   └── lib.rs          
├── my_crate_stubgen/
│   ├── Cargo.toml
│   └── src/
│       └── lib.rs

@Kile
Copy link

Kile commented Apr 13, 2025

Ah cool thanks

@AsherJingkongChen
Copy link

AsherJingkongChen commented Apr 23, 2025

Did you add this line to build.rs with feature = ["extension-module"] in Cargo.toml?

pyo3_build_config::add_extension_module_link_args();

I just reproduce the error by deleting the line, but it is no-op on Linux, lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants