【问题标题】:Portable binaries with Rust带有 Rust 的可移植二进制文件
【发布时间】:2014-11-29 21:38:47
【问题描述】:

我在使用 rust 构建可移植可执行文件时遇到问题。

在 Ubuntu 上运行使用 cargo build 简单构建的可执行文件失败

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

使用rustc ... -C link-args=-static 构建无法正确链接(ld ./test 的输出):

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

除了在具有旧 glibc 版本的旧系统上构建之外,还有其他方法吗?

【问题讨论】:

  • 您询问的是“便携式”二进制文件,但您只列出了一个系统 (Ubuntu),没有任何版本信息。是否涉及多个系统?如果有,它们的发行版及其版本是什么?

标签: ld static-linking rust rust-cargo


【解决方案1】:

为避免 GLIBC 错误,您可以针对静态替代 libc musl 编译您自己的 Rust 版本。

获取 musl 的最新稳定版本并使用选项--disable-shared 构建它:

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

然后针对 musl 构建 Rust:

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

然后构建你的项目

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

有关详细信息,请查看文档的 advanced linking 部分。

如原始文档中所述:

但是,您可能需要针对 musl 重新编译本机库 在它们可以链接之前。


您也可以使用rustup

删除由 rustup.sh 安装的旧 Rust

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

安装 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

【讨论】:

    【解决方案2】:

    Glibc 不是静态链接的(就像我们可能喜欢的那样,它竭尽全力防止这种情况发生)。因此,系统库(libstd 等)总是依赖于构建它们的 glibc 版本。这就是为什么 mozilla 使用的 linux 集群中的 buildbots 是/曾经是 centos 的旧版本。

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

    不幸的是,目前我认为除了确保您在具有比您要部署的旧 glibc 的系统上构建之外,没有其他解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 2013-03-01
      • 2015-01-12
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多