Cargo.toml to include LD_PRELOAD in cargo run?

Due to some third party library, I have a situation where: cargo run --example yay

works, while

cargo run --example yay



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.


Solutions involving, this_crate/.cargo/* are acceptable too. :slight_smile: [I can’t get it to work adding println! to, 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

man (8)

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:


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

Does adding this in your fix it?


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