[英]“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
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
.最重要的是dev
( development - 或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-level
和debuginfo
的值:
$ 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.