首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用“扩展模块”功能时PyO3链接器错误

使用“扩展模块”功能时PyO3链接器错误
EN

Stack Overflow用户
提问于 2022-04-04 22:23:38
回答 1查看 400关注 0票数 1

当我试图构建一个非常简单的PyO3模块时,编译会在链接阶段停止,并返回一条非常长的错误消息。我在Linux上编译了这段代码。

锈蚀项目如下所示:

代码语言:javascript
复制
├ Cargo.toml
└ src/
  └ lib.rs

Cargo.toml:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
use pyo3::prelude::*;

#[test]
fn test_print() {
    pyo3::prepare_freethreaded_python();
    Python::with_gil(|py| py.run( "print('Hello World')", None, None ) );
}

当我现在运行cargo test时,我得到一个链接器错误:

代码语言:javascript
复制
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机器上尝试此代码,有效。

我想我失去了某种依赖,但我无法弄清楚,缺少了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-05 19:34:36

看起来这是一个已知的PyO3问题。在他们的常见问题中提到了

Cargo.toml:

代码语言:javascript
复制
[dependencies.pyo3]
version = "0.16.3"

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

当扩展模块功能被激活时,cargo test在链接错误时失败.解决方法是使扩展模块特性可选,并使用

cargo test --no-default-features

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71744403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档