Cargo.toml to include LD_PRELOAD in cargo run?

Due to some third party library, I have a situation where:

LD_PRELOAD=foo.bar cargo run --example yay

works, while

cargo run --example yay

fails.

=====

The nice thing to do would be to figure out wtf is going on in the third party library, write a patch, and submit a pull request.

I’m lazy. Is thee a way to hard code the LD_PREOAD into Cargo.toml ? Right now, when using IntelliJ, my examples/unit-tests are running without the LD_PRELOAD (and crashes), which forces mt to manually open up the “runtime env” of each example/env, tell IntelliJ to add the LD_PRELOAD, … . I would prefer a way where I can just hard code this in Cargo.toml (or some other file) so that “cargo run” anywhere inside this crate always adds the LD_PRELOAD.

Thanks!

Solutions involving build.rs, this_crate/.cargo/* are acceptable too. :slight_smile: [I can’t get it to work adding println! to build.rs, and this_crate/.cargo/* seems to be getting ignored.]

LD_PRELOAD has nothing, nothing to do with rust itself. It’s part of your ELF loader (typically ld.so).

man (8) ld.so

A list of additional, user-specified, ELF shared objects to be loaded before all others. This feature can be used to selectively override functions in other shared objects.

(emphasis by me).

So, it might be an ordering problem, these are kinda hard to debug and find. The best way would be to create a core dump (if it really crashes with a SEGFAULT or similar) then then using a debugger to find out what exactly went wrong and where’s the difference to without using it.

To make it static, you could use your ~/.bashrc and put the LD_PRELOAD content there. But be aware, that this applies to every binary you run.

Yeah, I don’t want to add it to ~/.bashrc :slight_smile:

The library I am trying to load is:

~/.local/lib/python3.7/site-packages/torch/lib/libtorch.so

Otherwise, tch-rs complains that it can’t load CUDA.

Does adding this in your build.rs fix it?

println!("cargo:rustc-link-search=all=~/.local/lib/python3.7/site-packages/torch/lib/");

You could also add it to your .cargo/config like this