|
|
@ -1,3 +1,47 @@
|
|
1
|
1
|
# A journey through the Rust book
|
|
2
|
2
|
|
|
3
|
3
|
[This book.](http://doc.rust-lang.org/1.0.0-beta/book/)
|
|
|
4
|
|
|
|
5
|
## Compiling statically linked binaries
|
|
|
6
|
|
|
|
7
|
The following assumes you have a `main.rs` and want to get a statically
|
|
|
8
|
linked version of it in `main`. I'm not sure how this would work with
|
|
|
9
|
multiple files or `cargo`.
|
|
|
10
|
|
|
|
11
|
```
|
|
|
12
|
# collect `.rlib`s that need to be linked in later
|
|
|
13
|
$ rlibs=`rustc -Z print-link-args main.rs | sed -e 's/ /\n/g' | sed -e 's/"//g' | grep rlib`
|
|
|
14
|
$ echo rlibs
|
|
|
15
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib
|
|
|
16
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-4e7c5e5c.rlib
|
|
|
17
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode-4e7c5e5c.rlib
|
|
|
18
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-4e7c5e5c.rlib
|
|
|
19
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4e7c5e5c.rlib
|
|
|
20
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-4e7c5e5c.rlib
|
|
|
21
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-4e7c5e5c.rlib
|
|
|
22
|
|
|
|
23
|
# build `main.o`, i.e. compile `main.rs`, but do not link
|
|
|
24
|
$ rustc --emit obj main.rs
|
|
|
25
|
|
|
|
26
|
# link it statically (you need `librt.a` on your system, or in `./deps`)
|
|
|
27
|
# (also, the warnings seem to be expected)
|
|
|
28
|
$ gcc -static -static-libgcc -o main main.o `tr $'\n' ' '<<<$rlibs` -lpthread -lm -ldl -L./deps -lrt
|
|
|
29
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib(std-4e7c5e5c.o): In function `dynamic_lib::DynamicLibrary::open::h232b8007ebe62ccakTe':
|
|
|
30
|
std.0.rs:(.text._ZN11dynamic_lib14DynamicLibrary4open20h232b8007ebe62ccakTeE+0x103): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
|
|
|
31
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib(std-4e7c5e5c.o): In function `env::home_dir::h89e202bbf866699a0cf':
|
|
|
32
|
std.0.rs:(.text._ZN3env8home_dir20h89e202bbf866699a0cfE+0x14d): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
|
|
|
33
|
/home/lu/.local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib(std-4e7c5e5c.o): In function `net::lookup_host::h6fcc17ec54074bdeCik':
|
|
|
34
|
std.0.rs:(.text._ZN3net11lookup_host20h6fcc17ec54074bdeCikE+0x1ce): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
|
|
|
35
|
|
|
|
36
|
# and now you can run it!
|
|
|
37
|
$ file main
|
|
|
38
|
main: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=02abd2412f0570e81b4ffd8184edc883fb264f92, not stripped
|
|
|
39
|
$ ./main
|
|
|
40
|
Hello, World!
|
|
|
41
|
|
|
|
42
|
# it also works on other systems
|
|
|
43
|
$ docker run -it --rm -v $PWD/main:/usr/local/bin/hello busybox /usr/local/bin/hello
|
|
|
44
|
Hello, World!
|
|
|
45
|
```
|
|
|
46
|
|
|
|
47
|
Thanks to Kai Noda for [figuring this out](https://mail.mozilla.org/pipermail/rust-dev/2014-November/011365.html).
|