Statically link executable with glibc

ah crap, yea, cargo or rustc passes all kinds of things automatically, the only way to override that might be to make changes to their source

say, you really want this, for sake of demonstration—what seems to work is to make a staticlib crate

Cargo.toml:

…
[lib]
name = "statictest"
crate-type = ["staticlib"]

src/lib.rs:

#[no_mangle]
pub extern "C" fn main() {
    println!("Hell world");
}
$ cargo build --release
$ gcc -static target/release/libstatictest.a -o statictest -lpthread -ldl
$ ldd ./statictest
        not a dynamic executable
$ ./statictest
Hell world

(to be clear: it is unsafe to ship these executables to systems that don't have exactly the same glibc, as glibc will be dynamically loading nss modules internally and there might be ABI conflicts, even silent crashy ones, although this likely only matters if you do DNS lookups)

2 Likes