我的Rust项目使用了对C库SuperLU的FFI,它被称为superlu-sys.我的Ruust代码生成了与Py03的绑定.一旦Python绑定有一个调用SuperLU的函数,我就会在构建时收到以下链接器错误:
relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
这很奇怪,因为-fPIC是由构建来执行的.超级lu-sys的RS:
run!(cmd!("make")
.current_dir(&source.join("SRC"))
.arg("NOOPTS=-O0 -fPIC -w")
.arg("CFLAGS=-O3 -DNDEBUG -DPRNTlevel=0 -fPIC -w")
.arg("DZAUX=")
.arg("SCAUX=")
.arg(&format!("SuperLUroot={}", source.display()))
.arg(&format!(
"SUPERLULIB={}",
lib.join("libsuperlu.a").display()
)));
这是全部的build.rs美元.
在给出一个最小的例子后,我发现只要在构建SuperLu的同一个箱子里有Py03绑定,它就可以工作.我的印象是,只有当它是 crate 树的根时,才会给出对Build.rs旗帜的坚持.
如何修改构建.RS以获得-fPIC的一致实施?
这是导致该问题的最小示例: Lib.rs:
use pyo3::prelude::*;
use std::os::raw::c_int;
use superlu_sys;
#[pyclass]
pub struct Object {}
#[pymethods]
impl Object {
pub fn test(&self) -> i32 {
unsafe {
*superlu_sys::intMalloc(1 as c_int)
}
}
}
#[allow(unused)]
#[pymodule]
fn bind_superlu(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Object>()?;
Ok(())
}
货运量:
[package]
name = "bind_superlu"
version = "0.1.0"
edition = "2021"
[lib]
name = "ress"
crate-type = ["cdylib"]
[dependencies]
superlu-sys = "0.3.4"
pyo3 = {version = "0.18.1", features = ["auto-initialize"]}