Issue
My rust project uses a FFI to the C lib SuperLU, which is called superlu-sys. My rust code produces Python bindings with PyO3. As soon as the python bindings have a function calling SuperLU I get the following linker error on building:
relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
This is strange as -fPIC is enforced by build.rs of superlu-sys:
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()
)));
Here is the full build.rs.
On producing a minimum example I have found that it works as long as the PyO3 bindings are in the same crate in which SuperLu is built. I am under the impression that the adherence to the flags from build.rs is only given if it it the root of the crate tree.
How can I modify build.rs to get consistent enforcement of -fPIC?
This is a minimum example causing the issue: 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(())
}
cargo.toml:
[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"]}
Solution
I switched the buid.rs script to using the cmake rust crate instead of invoking make as a subprocess.
Answered By - Henrik Answer Checked By - Senaida (WPSolving Volunteer)