Reputation: 372
I want to create a static library using Rust, and then use it in a C program. The library is expected to be self-contained (i.e. all its dependencies are self-contained).
At first, crate-type = ["staticlib"]
was added in Cargo.toml
. But this methods seemed to worked only in simple cases, where Rust code only contains basic functions and no external crates.
For a more complex Rust code, the above method may fail to build a self-contained library. In this case, the library can be built, but it cannot be used by C program.
I also have learnt that one can build a self-contained library by typing
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target=x86_64-unknown-linux-gnu
However, the library failed to be built.
error: linking with `cc` failed: exit status: 1
|
= note: LC_ALL="C" PATH="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
/usr/bin/ld: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status> note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)>
I have also read the building part of the official instruction, from which I understand the native libraries should be linked to Rust cargo build, looking like
RUSTFLAGS='-C target-feature=+crt-static -L /path/to/nativelibrary' cargo build --release --target=x86_64-unknown-linux-gnu
Supposing that my Rust code uses dozens of external crates, each of which may also has its own dependencies, I really don't know what to fill in /path/to/nativelibrary
.
Questions:
/path/to/nativelibrary
is unavoidable, how to find out which native libraries to link?Upvotes: 2
Views: 1691