0

I would like to use two user generated .so library file in my rust code. These files are named lib_dens-mods.so and lib_twobody.so. My build.rs is the following

//build.rs
use std::env;
use std::process::Command;

fn main() {
    Command::new("sh")
        .arg("-c")
        .arg("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.")
        .status()
        .expect("failed to execute command");
    println!("cargo:rustc-link-search=./");
    
    // link against lib_dens-mods.so
    println!("cargo:rustc-link-search=lib_dens-mods");

    // link against lib_twobody.so
    println!("cargo:rustc-link-search=lib_twobody");
}

and my Cargo.toml is below

[package]
name = "pion-twobody"
authors = ["me myemail@mywebsite.xyz"]
version = "0.1.0"

[dependencies]
array2d = "0.3.0"
cc = "1.0.79"
lebedev_laikov= {git = "https://github.com/Rufflewind/lebedev_laikov.git", branch="master"}
libc = "0.2.144"
multiarray = "0.1.3"
num = "0.4.0"
# rand = "0.8.5"

[build-dependencies]
cc = "1.0.79"

But when I run this I get the following errors

error: linking with `cc` failed: exit status: 1

note: LC_ALL= (very long string)

note: /usr/bin/ld: cannot find -llib_dens-mods: No such file or directory
          /usr/bin/ld: cannot find -llib_twobody: No such file or directory
          collect2: error: ld returned 1 exit status

Within my rust code I am linking to the libraries using the lines

#[link(name = "lib_dens-mods")]
#[link(name = "lib_twobody")]

What can I do to solve these errors?

Alex Long
  • 55
  • 4
  • The script output should be `cargo:rustc-link-lib` rather than `cargo:rustc-link-search` when specifying the library name (the other is just for directories). But that shouldn't make a difference because [`#[link(...)]` will do that anyway](https://www.reddit.com/r/rust/comments/gal0f6/differences_between_buildrs_and_direct_use_of/) which is why you get that error at all. – kmdreko May 10 '23 at 17:28
  • 1
    The export that happens in the newly spawned shell does not propagate to the parent process. – Chayim Friedman May 10 '23 at 17:29
  • @kmdreko Thank you for this. Can you elaborate on how to fix it for my case? Apologies, I am new to Rust. – Alex Long May 10 '23 at 17:55
  • @ChayimFriedman Can you explain how make it work for the parent process as well? – Alex Long May 10 '23 at 18:31
  • You can use [`std::env::set_var()`](https://doc.rust-lang.org/stable/std/env/fn.set_var.html), but this will set it for the process of the build script. I don't think this will cause rustc to change its search path (I may be wrong though). – Chayim Friedman May 10 '23 at 18:36
  • Try to `println!("cargo:rustc-link-search=/absolute/path/to/the/folder/where/the/libs/are/");` instead of `./` – Jmb May 11 '23 at 07:05

0 Answers0