当我试图构建一个非常简单的PyO3模块时,编译会在链接阶段停止,并返回一条非常长的错误消息。我在Linux上编译了这段代码。
锈蚀项目如下所示:
├ Cargo.toml
└ src/
└ lib.rsCargo.toml:
[package]
name = "dytest"
version = "0.1.0"
edition = "2021"
[lib]
name = "dytest"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.16", features = ["extension-module"] }lib.rs:
use pyo3::prelude::*;
#[test]
fn test_print() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| py.run( "print('Hello World')", None, None ) );
}当我现在运行cargo test时,我得到一个链接器错误:
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" [...many, many paths...]
= note: /usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3_ffi::object::Py_DECREF':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-ffi-0.16.2/src/object.rs:407: undefined reference to `_Py_Dealloc'
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3_ffi::object::Py_None':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-ffi-0.16.2/src/object.rs:472: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3::err::PyErr::take':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.16.2/src/err/mod.rs:264: undefined reference to `PyErr_Fetch'
[...many, many error messages...]
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.15.rcgu.o): in function `pyo3::types::string::PyString::to_string_lossy':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.16.2/src/types/string.rs:199: undefined reference to `PyUnicode_AsEncodedString'
collect2: error: ld returned 1 exit status
= help: 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#cargorustc-link-libkindname)
error: could not compile `dytest` due to previous error如果我换掉
pyo3 = { version = "0.16", features = ["extension-module"] }
使用
pyo3 = { version = "0.16" }
一切都正常,但是PyO3用户指南显式地声明了这个特性。
在Windows机器上尝试此代码,有效。
我想我失去了某种依赖,但我无法弄清楚,缺少了什么。
发布于 2022-04-05 19:34:36
看起来这是一个已知的PyO3问题。在他们的常见问题中提到了
Cargo.toml:
[dependencies.pyo3]
version = "0.16.3"
[features]
extension-module = ["pyo3/extension-module"]
default = ["extension-module"]当扩展模块功能被激活时,cargo test在链接错误时失败.解决方法是使扩展模块特性可选,并使用
cargo test --no-default-features。
https://stackoverflow.com/questions/71744403
复制相似问题