简体   繁体   English

在发布模式下使用 rustc 编译时出现“无法识别的选项 'release'”

[英]“Unrecognized option 'release'” when compiling with rustc in release mode

I have a simple .rs file and want to compile it using rustc .我有一个简单的.rs文件,想使用rustc编译它。 I always heard about how important it is to compile in release mode, because otherwise my Rust program will be slow.我一直听说在发布模式下编译是多么重要,否则我的 Rust 程序会很慢。

However, if I use the often quoted --release flag, it doesn't work.但是,如果我使用经常引用的--release标志,则它不起作用。 What is everyone talking about if the flag doesn't even exist?如果国旗不存在,每个人都在谈论什么?

$ rustc --release foo.rs
error: Unrecognized option: 'release'.

You made a simple mistake: The --release flag is a flag for cargo .你犯了一个简单的错误: --release标志是cargo的标志。 The easiest way you can turn on optimizations with rustc is using the -O flag.使用rustc开启优化的最简单方法是使用-O标志。

Examples:例子:

rustc -O foo.rs
rustc -C opt-level=3 foo.rs
cargo build --release

A bit more detail:再详细一点:

You can compile your Rust program with various levels of optimization.您可以使用不同级别的优化来编译您的 Rust 程序。 rustc -C help says: rustc -C help说:

-C opt-level=val     -- optimize with possible levels 0-3, s, or z

To have the most control, you should compile with rustc -C opt-level=3 foo.rs (or any other level).为了获得最大的控制权,您应该使用rustc -C opt-level=3 foo.rs (或任何其他级别)进行编译。 However, this isn't always necessary.然而,这并不总是必要的。 Often, you can use -O ;通常,您可以使用-O rustc --help says: rustc --help说:

-O                      Equivalent to -C opt-level=2

Most of the time rustc -O foo.rs is the right choice.大多数时候rustc -O foo.rs是正确的选择。

Cargo, on the other hand, works a bit different;另一方面,Cargo 的工作方式有点不同。 or at least the --release flag does.或者至少--release标志可以。 Cargo has different profiles which dictate how cargo invokes rustc . Cargo 有不同的配置文件,这些配置文件决定了cargo如何调用rustc The most important ones are dev ( development - or debug -mode) and release .最重要的是devdevelopment - 或debug -mode)和release The --release flag switches the default profile from dev to release (duh!). --release标志将默认配置文件从dev切换到release (废话!)。 This potentially changes many flags of the rustc invocation.这可能会更改rustc调用的许多标志。 Most importantly, it changes the values for opt-level and debuginfo :最重要的是,它改变了opt-leveldebuginfo的值:

$ rustc -C debuginfo=2 -C opt-level=0    # in `dev` profile/debug mode
$ rustc -C debuginfo=0 -C opt-level=3    # in `release` profile

You can even change the settings for each profile in your Cargo.toml .您甚至可以更改Cargo.toml每个配置文件的设置。 You can find more information on profiles and on the default values used in this Cargo documentation .您可以在此 Cargo 文档 中找到有关配置文件和默认值的更多信息。

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

相关问题 为什么 rustc 没有将 libmariadb 包含到发布二进制文件中? - Why rustc did not include libmariadb into release binary? 我如何使用打印? 在编译发布时使用 Rocket? - How do I use print! with Rocket when compiling for release? 测试溢出时忽略 Cargo build --release 选项 - Cargo build --release option ignored when testing overflow 使用 rustc_serialize 编码 JSON 时省略 Option::None 值 - Omit values that are Option::None when encoding JSON with rustc_serialize 可以使用Cargo在发布模式下构建测试吗? - Can tests be built in release mode using Cargo? 在发布模式下编译时,为什么Godbolt编译器浏览器不显示我的函数的任何输出? - Why doesn't the Godbolt compiler explorer show any output for my function when compiled in release mode? 如何指定交叉编译rustc时要使用的编译器? - How do I specify the compiler to use when cross-compiling rustc? 使用 rustc 编译代码时如何指定要使用的版本? - How do I specify the edition to use when compiling code using rustc? Rust IO 在 --release 模式下非常快,但写入时间非常慢 - Rust IO when in --release mode incrediably fast, but writing time is horriably slow 释放模式下安全 Rust 中的有符号整数溢出是否被视为未定义行为? - Is signed integer overflow in safe Rust in release mode considered as undefined behavior?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM