I wrote simple code in C, Go and Rust.
foo.c
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
foo.go
package main
import "fmt"
func main() {
fmt.Println("hello");
}
foo.rs
fn main() {
println!("hello");
}
Then I built them all.
$ gcc -static -o cfoo foo.c
$ go build -o gofoo foo.go
$ rustc -o rustfoo foo.rs
They run fine.
$ ./cfoo; ./gofoo; ./rustfoo
hello
hello
hello
The binary of Rust executable was too small compared to the other two, so I suspected that it is not a static executable.
$ ls -l cfoo gofoo rustfoo
-rwxr-xr-x 1 lone lone 755744 Oct 23 21:17 cfoo
-rwxr-xr-x 1 lone lone 1906945 Oct 23 21:17 gofoo
-rwxr-xr-x 1 lone lone 253528 Oct 23 21:17 rustfoo
I confirmed that Rust does not produce a static executable.
$ ldd cfoo gofoo rustfoo
cfoo:
not a dynamic executable
gofoo:
not a dynamic executable
rustfoo:
linux-vdso.so.1 (0x00007ffe6dfb7000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd8d9b75000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd8d9b6b000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd8d9b4a000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd8d9b30000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd8d996f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd8d9bbb000)
Is there any way to produce a static executable for Rust?
I checked other similar answers and they talk about using musl. Is there no way to produce a static executable with glibc? If I must use an alternate way, can you provide a step by step with commands to produce a static executable with rustc
?
apt-cache search glibc | grep static
turns up no results. But there is/usr/lib/x86_64-linux-gnu/libc.a
on my system. Also, as explained in my question abovegcc -static -o cfoo foo.c
produces a static binary. So I am guessing static glibc is present on my system. Am I right? – Phycomycete