Portable rust binaries

I have problems creating a portable rust executable.

Running an executable just built with cargo build in Ubuntu fails with

 ./test: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.14' not found (required by ./test) 

The line with rustc ... -C link-args=-static is incorrectly connected ( ld ./test ):

 ld: error in ./test(.eh_frame); no .eh_frame_hdr table will be created. 

Is there any way around this other than building an older system with an older version of glibc?

+8
rust ld static-linking rust-cargo
source share
2 answers

Glibc is not statically linked (no matter how much we want, it is not able to prevent this). As a result, system libraries (libstd and those) always depend on the version of glibc on which they were created. This is why the buildbots in the linux cluster mozilla uses / were old versions of centos.

See https://github.com/rust-lang/rust/issues/9545 and https://github.com/rust-lang/rust/issues/7283

Unfortunately, at this time I believe that there is no workaround, except that you are building a system with an older glibc than you are going to deploy.

+3
source share

To avoid GLIBC errors, you can compile your own version of Rust against the static alternative libc, musl .

Get the latest stable version of musl and build it with the --disable-shared option:

 $ mkdir musldist $ PREFIX=$(pwd)/musldist $ ./configure --disable-shared --prefix=$PREFIX 

then create Rust against musl:

 $ ./configure --target=x86_64-unknown-linux-musl --musl-root=$PREFIX --prefix=$PREFIX 

then create your project

 $ echo 'fn main() { println!("Hello, world!"); }' > main.rs $ rustc --target=x86_64-unknown-linux-musl main.rs $ ldd main not a dynamic executable 

For more information, see the extended link section in the documentation.

As indicated in the source documentation:

However, you may need to recompile your native anti-muslin libraries before they can be linked.


You can also use rustup .

Remove the old rust installed by rustup.sh

 $ sudo /usr/local/lib/rustlib/uninstall.sh # only if you have $ rm $HOME/.rustup 

Install rustup

 $ curl https://sh.rustup.rs -sSf | sh $ rustup default nightly #just for ubuntu 14.04 (stable Rust 1.11.0 has linking issue) $ rustup target add x86_64-unknown-linux-musl $ export PATH=$HOME/.cargo/bin:$PATH $ cargo new --bin hello && cd hello $ cargo run --target=x86_64-unknown-linux-musl $ ldd target/x86_64-unknown-linux-musl/debug/hello not a dynamic executable 
-one
source share

All Articles