Cargo: How to build external static lib only when testing?



I am trying to use cargo to test and build a lib. My tests resides in tests/ sub folders. Some of my tests need to calls external c function which resides in file say foo.c . What I want is that before cargo build/run my test , it should first build libfoo.a from foo.c and link against these tests.

I can use build = “” for building external libs to be linked with main lib, but I do not know how to build external libs to be linked with tests.



I think I have figured out how to do this , hope following steps will help some one.

Say I am building a lib crate called “system” and I have a test file in tests sub folder i.e tests/ This make use of external c functions which resides in “foo,c” . So now when test is built I want it to link with libfoo.a

Add link attribute to
#[link = "foo" , kind = "static"]

Create as given in , except for last line and paths

use std::process::Command;
use std::env;
use std::path::Path;

fn main () {
  let out_dir = env::var("OUT_DIR").unwrap();
  Command::new("gcc").args(&["tests/foo.c", "-c", "-fPIC", "-o"])
    .arg(&format!("{}/foo.o", out_dir))
  Command::new("ar").args(&["crus", "libfoo.a", "foo.o"])

  println!("cargo:rustc-link-search=native={}", out_dir);


Add to Cargo.toml

name = "..."
build = ""

Now cargo build will not link against libfoo.a , but for cargo test , the test files which include the #[link = “foo” , kind = “static”] will now link against libfoo.a