How do you specify the LD_LIBRARY_PATH for a rust project in IntelliJ IDEA?

Viewed 1736

I am using IntelliJ IDEA to experiment with rust. In order to gain an understanding of how to invoke libraries created in other languages, I have created a tiny dynamic library in C whose .so file is in ../expclib1/libexp1.so.

How do I inform intellij and rust that the implementation of

#[link(name="exp1")]
extern {
    fn bacon()->i32;
}

lives in ../expclib1/libexp1.so ? I know that in C you need to specify -L../expclib1 -lexp1 in the link stage and then export LD_LIBRARY_PATH=../expclib1 before trying to run it. How do I make this work from cargo, and how do I make it work from intellij's run menu? There are basically no options on the module inside of intellij, so I'm hoping that it can all be specified in the Cargo.toml.

1 Answers

Extra link arguments for rust can be specified using the build.rs file which usually lives next to Cargo.toml. Here is an example build.rs:

fn main() {
    println!("cargo:rustc-link-search=../expclib1\n\
    cargo:rustc-link-search=/opt/oracle-jdk-bin-1.8.0.202/jre/lib/amd64/jli\n\
    cargo:rustc-link-search=/opt/oracle-jdk-bin-1.8.0.202/jre/lib/amd64/server\n\
    cargo:rustc-link-lib=dylib=exp1\n\
    cargo:rustc-link-lib=dylib=jli\n\
    cargo:rustc-link-lib=dylib=jvm");
}

That example should probably be a little smarter about finding the jni and jvm libraries, because not every system will be running java 1.8.0.202 installed in /opt.

The settings cargo uses from the output are documented in the Build Scripts section of the rust documentation.

That only enables the application to link. To enable it to run you must modify IntelliJ's run configuration by setting an environment variable like so:

setting environment variables for a run configuration

Related