[英]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 可执行文件?
You need to compile for MUSL, otherwise Rust will depend on your libc.您需要针对 MUSL 进行编译,否则 Rust 将取决于您的 libc。
https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html
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.