How to link static library to executable?

I have custom static library libmultiply.a that is placed inside of my project. I want to specify path to that library in Cargo.toml so it can be linked to the FFI code. How can I do that?

To point you in the right direction, you need to include a build.rs script to tell cargo about extra things to link.

https://doc.rust-lang.org/stable/cargo/reference/build-scripts.html#rustc-link-lib

@alice Thanks. That works (despite the fact it seems a little wierd to write a build script only for one compiler option). I just thought that there is a way to do that in Cargo.toml file.

Most of the time you will be compiling the dependency from source or shell out to a tool like pkg-config to find the library, so having an executable program for figuring out the arguments to pass rustc makes sense.

It's not often that you'll be able to use a hard-coded path, and that wouldn't work in a cross-platform way because (for example) different platforms compile their libraries differently and using pre-compiled binaries would require the ability to specify different paths.

1 Like

@Michael-F-Bryan nice explanation, thanks.