为什么 Rust 可执行文件如此庞大? [英] Why are Rust executables so huge?

查看:64
本文介绍了为什么 Rust 可执行文件如此庞大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚找到 Rust 并阅读了文档的前两章,我发现他们定义语言的方法和方式特别有趣.所以我决定弄湿我的手指并开始使用 Hello world ...

Just having found Rust and having read the first two chapters of the documentation, I find the approach and the way they defined the language particularly interesting. So I decided to get my fingers wet and started out with Hello world...

顺便说一句,我是在 Windows 7 x64 上这样做的.

I did so on Windows 7 x64, btw.

fn main() {
    println!("Hello, world!");
}

发出 cargo build 并查看 targets\debug 中的结果,我发现结果 .exe 为 3MB.经过一番搜索(很难找到货物命令行标志的文档......)我找到了 --release 选项并创建了发布版本.令我惊讶的是,.exe 的大小只变小了一点点:2.99MB 而不是 3MB.

Issuing cargo build and looking at the result in targets\debug I found the resulting .exe being 3MB. After some searching (documentation of cargo command line flags is hard to find...) I found --release option and created the release build. To my surprise, the .exe size has only become smaller by an insignificant amount: 2.99MB instead of 3MB.

因此,承认我是 Rust 及其生态系统的新手,我的期望是系统编程语言会产生一些紧凑的东西.

So, confessing I am a newbie to Rust and its ecosystem, my expectation would have been that a Systems Programming language would produce something compact.

谁能详细说明 Rust 编译成什么,它怎么可能从 3 行程序生成如此巨大的图像?是编译成虚拟机吗?是否有我错过的 strip 命令(发布版本中的调试信息?)?还有什么可以让我们了解正在发生的事情的吗?

Can anyone elaborate on what Rust is compiling to, how it can be possible it produces such huge images from a 3 liner program? Is it compiling to a virtual machine? Is there a strip command I missed (debug info inside the release build?)? Anything else which might allow to understand what is going on?

推荐答案

Rust 使用静态链接来编译它的程序,这意味着即使是最简单的 Hello world! 程序所需的所有库都将被编译成你的可执行文件.这也包括 Rust 运行时.

Rust uses static linking to compile its programs, meaning that all libraries required by even the simplest Hello world! program will be compiled into your executable. This also includes the Rust runtime.

要强制 Rust 动态链接程序,请使用命令行参数 -C prefer-dynamic;这将导致文件更小还需要 Rust 库(包括其运行时)在运行时可供您的程序使用.这实质上意味着如果计算机没有它们,您将需要提供它们,这比您原来的静态链接程序占用的空间要多.

To force Rust to dynamically link programs, use the command-line arguments -C prefer-dynamic; this will result in a much smaller file size but will also require the Rust libraries (including its runtime) to be available to your program at runtime. This essentially means you will need to provide them if the computer does not have them, taking up more space than your original statically linked program takes up.

为了可移植性,如果您要将程序分发给其他人,我建议您按照您一直在做的方式静态链接 Rust 库和运行时.

For portability I'd recommend you statically link the Rust libraries and runtime in the way you have been doing if you were to ever distribute your programs to others.

这篇关于为什么 Rust 可执行文件如此庞大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆