Building a Fully Static Linux Executable in 2021
Hello everyone. With all the development going on in the Rust compiler, many advice pertaining to outputting a static Linux executable is outdated or replaced by ugly hacks. Many Rust CLI tools distribute static executables, a good example of this is ripgrep (in its musl binary).
My goal is to have a binary that has the following information displayed by file.
my_application: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
My current approach is the following command. It works fine, but God is it ugly.
RUSTFLAGS='-C linker=ld.lld -C relocation-model=static -C strip=symbols' cargo build --release --target x86_64-unknown-linux-musl
My question is what is the cleanest way to produce a statically linked Linux binary in 2021 that meets the following requirements?
- Reproduces the above
fileoutput - A clean command
- Compatible with other Cargo features
- Uses musl libc
Thanks!