简体   繁体   English

如何使用 glibc 而不是 musl 创建带有 rustc 的 static 可执行文件?

[英]How can I create a static executable with rustc using glibc instead of musl?

I wrote simple code in C, Go and Rust.我在 C、Go 和 Rust 中编写了简单的代码。

foo.c foo.c

#include <stdio.h>

int main()
{
    printf("hello\n");
    return 0;
}

foo.go foo.go

package main

import "fmt"

func main() {
    fmt.Println("hello");
}

foo.rs foo.rs

fn main() {
    println!("hello");
}

Then I built them all.然后我把它们都建好了。

$ gcc -static -o cfoo foo.c
$ go build -o gofoo foo.go
$ rustc -o rustfoo foo.rs

They run fine.他们运行良好。

$ ./cfoo; ./gofoo; ./rustfoo
hello
hello
hello

The binary of Rust executable was too small compared to the other two, so I suspected that it is not a static executable. Rust 可执行文件的二进制文件与其他两个相比太小,所以我怀疑它不是 static 可执行文件。

$ 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

I confirmed that Rust does not produce a static executable.我确认 Rust 不会产生 static 可执行文件。

$ ldd cfoo gofoo rustfoo
cfoo:
    not a dynamic executable
gofoo:
    not a dynamic executable
rustfoo:
    linux-vdso.so.1 (0x00007ffe6dfb7000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd8d9b75000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd8d9b6b000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd8d9b4a000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd8d9b30000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd8d996f000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd8d9bbb000)

Is there any way to produce a static executable for Rust?有没有办法为 Rust 生成 static 可执行文件?

I checked other similar answers and they talk about using musl.我检查了其他类似的答案,他们谈论使用 musl。 Is there no way to produce a static executable with glibc?有没有办法用 glibc 生成 static 可执行文件? If I must use an alternate way, can you provide a step by step with commands to produce a static executable with rustc ?如果我必须使用另一种方法,您能否提供一步一步的命令来生成带有rustc的 static 可执行文件?

It seems things may have changed since this was originally answered.自从最初回答这个问题以来,似乎事情可能已经发生了变化。

First, you need to make sure you have a statically linked glibc available on your system.首先,您需要确保您的系统上有一个静态链接的 glibc。 I'm using a RHEL system, so I did that with:我正在使用 RHEL 系统,所以我这样做了:

sudo yum install glibc-static

I believe the Ubuntu / Debian equivalent is:我相信 Ubuntu / Debian 等价物是:

sudo apt-get install libc6-dev

After that, you pass the option -C target-feature=+crt-static to rustc when compiling your program:之后,在编译程序时将选项-C target-feature=+crt-static传递给 rustc:

$ rustc -o rustfoo -C target-feature=+crt-static main.rs
$ ldd rustfoo
        not a dynamic executable

Of course few people run rustc by hand.当然很少有人手动运行 rustc。 You can instruct cargo to pass this option to rustc using the RUSTFLAGS environment variable, like this:您可以使用RUSTFLAGS环境变量指示 cargo 将此选项传递给 rustc,如下所示:

RUSTFLAGS="-C target-feature=+crt-static" cargo build --target x86_64-unknown-linux-gnu

The reason you have to add the --target option, even if you are building for the host platform, is that if you don't then the options supplied via RUSTFLAGS will be applied when building compile time code such as proc macros, etc which may cause those to fail to compile.即使您正在为主机平台构建,您必须添加--target选项的原因是,如果您不这样做,那么在构建编译时代码(例如 proc 宏等)时将应用通过RUSTFLAGS提供的选项可能导致那些无法编译。 If you explicitly specify a target platform then the RUSTFLAGS are only applied when compiling code for the target platform.如果您明确指定目标平台,则RUSTFLAGS仅在为目标平台编译代码时应用。 See also this bug report .另请参阅此错误报告

If you want it to always build statically like this, without needing an environment variable, then you can create a file .cargo/config.toml within your project top directory, and put the following content into it:如果您希望它始终像这样静态构建,而不需要环境变量,那么您可以在项目顶级目录中创建一个文件.cargo/config.toml ,并将以下内容放入其中:

[build]
rustflags = ["-C", "target-feature=+crt-static"]
target = "x86_64-unknown-linux-gnu"

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用rustc生成可执行文件? - How to produce executable with rustc? 我如何查询主机三元组的 rustc? - How can I query rustc for the host triple? 如何仅使用 cargo 或 rustc 为自定义目标编译 Rust 核心板条箱? - How can I compile the Rust core crate for a custom target using only cargo or rustc? 如何定位我的 Rust 应用程序及其对 musl 的依赖项? - How can I target my Rust application and it's dependencies to musl? 当rustc启用musc时,未能使用货物建立rust-libc - failure to build rust-libc using cargo when rustc is musl-enabled 如何编译具有本机依赖项的 Rust 项目的静态 musl 二进制文件? - How to compile a static musl binary of a Rust project with native dependencies? 我如何使“ rustc”的注释静音,以免链接到哪些本地工件? - How can I silence `rustc`'s note about which native artifacts to link against? 使用 rustc 编译代码时如何指定要使用的版本? - How do I specify the edition to use when compiling code using rustc? 如果标记包含空格,如何将标记从构建脚本传递给rustc? - How can I pass flags to rustc from a build script if they contain a space? 如何在现有项目中对Cargo进行类似的依赖解析,并且只使用rustc进行代码分析? - How can I get similar dependency resolution to Cargo in an existing project with only rustc for code analysis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM