Cargo test could not link to libv8

This is my build script. cargo build build and generates the bindings.

extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main() {
    println!("cargo:rustc-link-lib=libv8");
    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .enable_cxx_namespaces()
        .generate()
        .expect("Could not generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings.write_to_file(out_path.join("bindings.rs"))
        .expect("Could not write bindings");
}

But when I cargo test, it gives the following error:
error: linking with cc failed: exit code: 1
note: /usr/bin/ld: cannot find -llibv8

println!("cargo:rustc-link-lib=libv8");

This should be v8 instead of libv8. (The linker will automatically translate the library name v8 to a filename like libv8.so.)

1 Like

Awesome. Thanks. :smile:
I have a question. Previously, it worked for build but not for test. What different was happening in that case?

Intermediate libraries don't actually link to other libraries - that's deferred until a final executable, like the test harness, is built.

2 Likes