如何使用 glibc 而不是 musl 使用 rustc 创建静态可执行文件?

Lon*_*ner 8 runtime libc static-linking rust

我用 C、Go 和 Rust 编写了简单的代码。

foo.c

#include <stdio.h>

int main()
{
    printf("hello\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

foo.go

#include <stdio.h>

int main()
{
    printf("hello\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

foo.rs

package main

import "fmt"

func main() {
    fmt.Println("hello");
}
Run Code Online (Sandbox Code Playgroud)

然后我把它们全部建造起来。

fn main() {
    println!("hello");
}
Run Code Online (Sandbox Code Playgroud)

他们运行良好。

$ gcc -static -o cfoo foo.c
$ go build -o gofoo foo.go
$ rustc -o rustfoo foo.rs
Run Code Online (Sandbox Code Playgroud)

Rust 可执行文件的二进制文件与其他两个相比太小,因此我怀疑它不是静态可执行文件。

$ ./cfoo; ./gofoo; ./rustfoo
hello
hello
hello
Run Code Online (Sandbox Code Playgroud)

我确认 Rust 不会生成静态可执行文件。

$ 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
Run Code Online (Sandbox Code Playgroud)

有没有办法为 Rust 生成静态可执行文件?

我检查了其他类似的答案,他们谈论使用 musl。有没有办法用 glibc 生成静态可执行文件?如果我必须使用替代方法,您能否提供一步一步的命令来生成静态可执行文件rustc

har*_*mic 20

自从最初回答这个问题以来,事情似乎已经发生了变化。

首先,您需要确保系统上有可用的静态链接 glibc。我使用的是 RHEL 系统,所以我这样做了:

sudo yum install glibc-static
Run Code Online (Sandbox Code Playgroud)

我相信 Ubuntu / Debian 的等价物是:

sudo apt-get install libc6-dev
Run Code Online (Sandbox Code Playgroud)

之后,-C target-feature=+crt-static在编译程序时将选项传递给 rustc:

$ rustc -o rustfoo -C target-feature=+crt-static main.rs
$ ldd rustfoo
        not a dynamic executable
Run Code Online (Sandbox Code Playgroud)

当然很少有人手动运行 rustc。您可以指示 Cargo 使用环境变量将此选项传递给 rustc RUSTFLAGS,如下所示:

RUSTFLAGS="-C target-feature=+crt-static" cargo build --target x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)

--target即使您正在为主机平台构建,您也必须添加该选项的原因是,如果您不这样做,那么RUSTFLAGS在构建编译时代码(例如 proc 宏等)时将应用通过提供的选项,这可能会导致这些选项编译失败。如果您显式指定目标平台,则RUSTFLAGS仅在为目标平台编译代码时应用。另请参阅此错误报告

如果您希望它始终像这样静态构建,而不需要环境变量,那么您可以.cargo/config.toml在项目顶级目录中创建一个文件,并将以下内容放入其中:

[build]
rustflags = ["-C", "target-feature=+crt-static"]
target = "x86_64-unknown-linux-gnu"
Run Code Online (Sandbox Code Playgroud)

参考:

  • 惊人的!目前它在 proc-macro crates 上遇到了问题。解决方法是始终指定目标`RUSTFLAGS =“-C target-feature = + crt-static”cargo build --target x86_64-unknown-linux-gnu --release`。更多关于 github 上的问题:https://github.com/rust-lang/rust/issues/78210 (2认同)