// build.rs
fn main() {
// tell rustc to link with some libhello.a library
println!("cargo:rustc-link=hello");
// and it should search the Cargo.toml directory for that library
println!("cargo:rustc-link-search={}", std::env::var("CARGO_MANIFEST_DIR").unwrap());
}
Then I used this to compile and run:
$ clang -c hello.c -o hello.o
$ ar rc libhello.a hello.o
$ ls
build.rs Cargo.toml hello.c hello.o libhello.a src
$ cargo run
Compiling repro v0.1.0 (/tmp/repro)
Finished dev [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/repro`
Hello!
Can you specify what "cannot be compiled" means? Are you getting compilation errors from Visual C++, is the linker complaining of missing symbols or unused arguments, etc.?
[package]
name = "hellocall"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
build = "build.rs"
[dependencies]
build.rs
fn main() {
// tell rustc to link with some libhello.a library
println!("cargo:rustc-link=hello");
// and it should search the Cargo.toml directory for that library
println!("cargo:rustc-link-search={}", std::env::var("CARGO_MANIFEST_DIR").unwrap());
}
I still can't compile. The error message is the same as before. It is still a warning that hello.lib can't be found, and then there is an error that hello can't be found.
I work with VSCode on the Windows 10. This may be different from your compilation environment.
As mentioned in the question, initially, I compiled hellolib.lib with Visual Studio 2013. Then copy hellolib.lib to the src folder. The code build.rs is not used. the Rust code main.rs compiled with VSCode. The compilation process prompted that the hellolib.lib file could not be found.