13

Is there any way to tell Rust where to look for my static library? Example code

#[link(name = "/this/is/the/path/libfoo.a", kind = "static")]

If not, what config change can I make or what folder do I place my library in so that I can use it?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
goo
  • 2,230
  • 4
  • 32
  • 53

2 Answers2

27

rustc invokes the system linker which looks for all libraries specified in #[link(...)] in library directories. There are usually several default library directories (like /lib and /usr/lib on Linux), and more can be specified via linker flags (rustc accepts -L options which it then passes through to the linker).

If you invoke rustc directly, you can use the -L option to add additional library directories which will be then passed through to the linker. If you use Cargo, however, you've got a few more options:

  • Cargo adds the /target/<profile>/deps directory as a library source directory.

  • You can use cargo rustc

    cargo rustc -- -L /path/to/library/directory 
    
  • You can specify the RUSTFLAGS environment variable:

    RUSTFLAGS='-L /path/to/library/directory' cargo build
    
  • You can use a build script to output more linker options

    println!("cargo:rustc-link-lib=static=foo");
    println!("cargo:rustc-link-search=native=/path/to/foo");
    

The easiest way for you, I think, is to add a custom build script which will copy or create a symlink to your library in the corresponding /target/<profile>/deps directory.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 1
    Yes, I'm using Cargo but after placing my libs inside `/build`, `/build/native/*`, & `/deps`, I still can not get it to work :( Error: `could not find native static library 'demo', perhaps an -L flag is missing?`. My lib name is `libdemo.a` – goo Oct 08 '14 at 15:30
  • 1
    I'm very sorry, I meant `target/`, not `build/`. I don't know why I wrote `build/` :( Also you can't just put your files there, this directory is a kind of erased before each build. You need the build script to do it for you. See [here](https://github.com/netvl/pub-rust-cargo-external-libs-example), for example. – Vladimir Matveev Oct 08 '14 at 16:28
0

To add on to the accepted answer, what worked for me is as following:

  • under debug build, putting dependency files under target/debug/deps worked; but putting files under target/debug/native/* did not seem to work.

Cargo seems to only look under target/debug/deps by default.

You can run with cargo build --verbose to see the verbose rustc commands and the options used. -L option specifies additional link dependency directory.

Henry Luo
  • 354
  • 4
  • 10
  • `cargo build --verbose` [doesn't show the actual linker command](https://github.com/rust-lang/rust/issues/38206), so that's of dubious usefulness. Anyway, I've updated the accepted answer to no longer be factually inaccurate. – Shepmaster Apr 21 '18 at 15:20
  • With `cargo` _(v0.26.0)_ that I'm using, it does indicate the `-L` option for the `rustc` command under `verbose` mode. The [github issue](https://github.com/rust-lang/rust/issues/38206) is probably already fixed. – Henry Luo Apr 30 '18 at 04:27
  • No, that's the command *to `rustc`*, not the command *to the linker*. – Shepmaster Apr 30 '18 at 16:37