I believe it is not possible to link to glibc statically. My understanding is that to get a Rust binary that does not depend on a system-provided libc at all we need to add explicit support for alternate libc implementations[1].

[1]: https://github.com/rust-lang/rust/issues/7283

On 11/15/2014 01:02 PM, Kai Noda wrote:
Hi Paul,

I managed to get it working by manually tweaking linker options (this mustn't be the right way to go...)

Rust devs: what is the official way to do this? Simply adding "-C link-args=-static" doesn't work (see my second transcript)

$ cat hello-rust.rs <http://hello-rust.rs>
fn main() {
    println!("Hello, world!");
}
$ rlibs=`rustc -Z print-link-args hello-rust.rs <http://hello-rust.rs> | sed -e 's/ /\n/g' | grep rlib`
$ echo $rlibs
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libnative-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libsync-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustrt-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode-4e7c5e5c.rlib'
'/home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-4e7c5e5c.rlib'
$ rustc -Z print-link-args --emit obj hello-rust.rs <http://hello-rust.rs>
$ file hello-rust.o
hello-rust.o: ELF 64-bit LSB relocatable, x86-64, version 1 (GNU/Linux), not stripped $ eval "gcc -static -static-libgcc hello-rust.o `tr $'\n' ' '<<<$rlibs` -lpthread -lm -ldl -lrt -o hello-rust" /home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib(std-4e7c5e5c.o): In function `dynamic_lib::dl::open_internal::hda8e305d06a14b844og': std.0.rs:(.text._ZN11dynamic_lib2dl13open_internal20hda8e305d06a14b844ogE+0x8): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /home/nodakai/local/rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib(std-4e7c5e5c.o): In function `io::net::addrinfo::get_host_addresses::hf208fefd6f07b89awSi': std.0.rs:(.text._ZN2io3net8addrinfo18get_host_addresses20hf208fefd6f07b89awSiE+0xad): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$ file ./hello-rust
./hello-rust: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x7fa2ba5ebdec633b220a8e9c988c4632b26a17d9, not stripped
$ ./hello-rust
Hello, world!
$ ls -l hello-rust
-rwxrwxr-x 1 nodakai nodakai 4.3M 11月 16 04:41 hello-rust*


$ x=`rustc -Z print-link-args hello-rust.rs <http://hello-rust.rs>`
$ y=`rustc -C link-args=-static -Z print-link-args hello-rust.rs <http://hello-rust.rs>`
$ diff <(tr ' ' $'\n' <<< $x) <(tr ' ' $'\n' <<< $y)
13d12
< '-pie'
39a39
> '-static'
$ file hello-rust
hello-rust: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x3a723f38042703be0ad45679b53220ce5f21787d, not stripped
$ LANG=C ./hello-rust
zsh: no such file or directory: ./hello-rust (<-- ENOENT, ie. invalid executable)

Regards,
Kai

野田  開 <noda...@gmail.com <mailto:noda...@gmail.com>>

2014-11-16 0:40 GMT+08:00 Paul Colomiets <p...@colomiets.name <mailto:p...@colomiets.name>>:

    Hi,

    Is there any way to compile static binary with rust compiler? I'm
    experimenting with linux conainerisation and it would be much easier
    if I could compile a binary which doesn't depend on libc.

    There is also an stack overflow question with no answer:
    http://stackoverflow.com/questions/26202494/portable-binaries-with-rust

    Thanks in advance!

    --
    Paul
    _______________________________________________
    Rust-dev mailing list
    Rust-dev@mozilla.org <mailto:Rust-dev@mozilla.org>
    https://mail.mozilla.org/listinfo/rust-dev




_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to