我发现他们在文档的前两章中定义语言的方法和方式特别有趣。所以我决定试一试,从“你好,世界!”开始。
顺便说一句,我是在Windows 7 x64上这么做的。
fn main() {
println!("Hello, world!");
}
发布cargo build并在目标\调试中查看结果,我发现结果的.exe为3MB。经过一番搜索(cargo命令行标志的文档很难找到…)我找到了——release选项并创建了发布版本。令我惊讶的是,.exe的大小只变小了一点:2.99MB而不是3MB。
我的期望是系统编程语言会产生一些紧凑的东西。
谁能详细解释一下Rust编译的目的是什么,它是如何从一个3行程序生成如此巨大的图像的?它是否编译到虚拟机?是否有我错过的strip命令(发布版本中的调试信息?)?还有什么能让我们理解的吗?
每晚安装防锈装置
rust工具链每晚安装,rust默认每晚安装
现在,在所有Cargo中进行这些更改。项目中的Toml文件。
在Cargo.toml的顶部[package]之前添加cargo-features = ["strip"]
在底部,或者在[dependencies]和[package]之间添加,
[profile.release]
# strip = true # Automatically strip symbols from the binary.
opt-level = "z" # Optimize for size.
lto = true # Enable link time optimization
codegen-units = 1 # Reduce parallel code generation units
现在使用RUSTFLAGS='-C link-arg=-s'构建货物——发布
我发现这些链接很有用——https://collabora.com/news-and-blog/blog/2020/04/28/reducing-size-rust-gstreamer-plugin/、https://github.com/johnthagen/min-sized-rust和https://arusahni.net/blog/2020/03/optimizing-rust-binary-size.html
每晚安装防锈装置
rust工具链每晚安装,rust默认每晚安装
现在,在所有Cargo中进行这些更改。项目中的Toml文件。
在Cargo.toml的顶部[package]之前添加cargo-features = ["strip"]
在底部,或者在[dependencies]和[package]之间添加,
[profile.release]
# strip = true # Automatically strip symbols from the binary.
opt-level = "z" # Optimize for size.
lto = true # Enable link time optimization
codegen-units = 1 # Reduce parallel code generation units
现在使用RUSTFLAGS='-C link-arg=-s'构建货物——发布
我发现这些链接很有用——https://collabora.com/news-and-blog/blog/2020/04/28/reducing-size-rust-gstreamer-plugin/、https://github.com/johnthagen/min-sized-rust和https://arusahni.net/blog/2020/03/optimizing-rust-binary-size.html
#![no_main]
#![no_std]
#[link(name = "msvcrt", kind = "dylib")]
extern {
fn puts(ptr: *const u8); // i8 or u8 doesn't matter in this case
}
#[no_mangle]
unsafe extern fn main() {
puts("Hello, World!\0".as_ptr());
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
下一个配置文件
[profile.release]
debug = false
strip = true
opt-level = 'z'
codegen-units = 1
lto = true
panic = 'abort'
用-r给出9 kb,而C
#include <stdio.h>
main() {
puts("Hello, World!");
}
GCC和-Os给出48 kb, TCC给出2 kb。令人印象深刻,不是吗?