rustc 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

rustc 命令安装:


-bash/zsh: rustc command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install rustc

# Debian
apt-get install rustc

# Ubuntu
apt-get install rustc

# Alpine
apk add rust

# Arch Linux
pacman -S rust

# Kali Linux
apt-get install rustc

# Fedora
dnf install rust

# OS X
brew install rust

# Raspbian
apt-get install rustc

# Dockerfile
dockerfile.run/rustc

# Docker
docker run cmd.cat/rustc rustc

rustc 命令补充说明:


rustc 是 Rust 语言的编译器,可在 https://www.rust-lang.org 上找到。

rustc 命令语法:


rustc [OPTIONS] INPUT

rustc 命令选项:


-h, --help
Display the help message.
--cfg SPEC
Configure the compilation environment.
-L [KIND=]PATH
   Add a directory to the library search path. The optional KIND can be one of:
   dependency
      only lookup transitive dependencies here
   crate
      only lookup local `extern crate` directives here
   native
      only lookup native libraries here
   framework
      only look for OSX frameworks here
   all
      look for anything here (the default)

-l [KIND=]NAME
   Link the generated crate(s) to the specified library NAME. The optional KIND can be one of static, dylib, or framework. If omitted, dylib is assumed.
--crate-type [bin|lib|rlib|dylib|cdylib|staticlib|proc-macro]
   Comma separated list of types of crates for the compiler to emit.
--crate-name NAME
   Specify the name of the crate being built.
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir][=PATH]
   Configure the output that rustc will produce. Each emission may also have an optional explicit output PATH specified for that particular emission kind. This path takes precedence over the -o option.
--print [crate-name|file-names|sysroot|target-libdir|cfg|target-list|target-cpus|target-features|relocation-models|code-models|tls-models|target-spec-json|native-static-libs|stack-protector-strategies|link-args]
   Comma separated list of compiler information to print on stdout.
-g
   Equivalent to -C debuginfo=2.
-O
   Equivalent to -C opt-level=2.
-o FILENAME
   Write output to FILENAME. Ignored if multiple --emit outputs are specified which don't have an explicit path otherwise.
--out-dir DIR
   Write output to compiler‐chosen filename in DIR. Ignored if -o is specified. Defaults to the current directory.
--explain OPT
   Provide a detailed explanation of an error message.
--test
   Build a test harness.
--target TARGET
   Target triple for which the code is compiled. This option defaults to the host’s target triple. The target triple has the general format <arch><sub>-<vendor>-<sys&  gt;-<abi>, where:

   <arch>
      x86, arm, thumb, mips, etc.
   <sub>
      for example on ARM: v5, v6m, v7a, v7m, etc.
   <vendor>
      pc, apple, nvidia, ibm, etc.
   <sys>
      none, linux, win32, darwin, cuda, etc.
   <abi>
      eabi, gnu, android, macho, elf, etc.
-W help
   Print 'lint' options and default settings.
-W OPT, --warn OPT
   Set lint warnings.
-A OPT, --allow OPT
   Set lint allowed.
-D OPT, --deny OPT
   Set lint denied.
-F OPT, --forbid OPT
   Set lint forbidden.
-C FLAG[=VAL], --codegen FLAG[=VAL]
   Set a codegen‐related flag to the value specified. Use -C help to print available flags. See CODEGEN OPTIONS below.
-V, --version
   Print version info and exit.
-v, --verbose
   Use verbose output.
--remap-path-prefix from=to
   Remap source path prefixes in all output, including compiler diagnostics, debug information, macro expansions, etc. The from=to parameter is scanned from right to left, so from may contain '=', but to may not.

   This is useful for normalizing build products, for example by removing the current directory out of pathnames emitted into the object files. The replacement is purely textual, with 
   no consideration of the current system's pathname syntax. For example --remap-path-prefix foo=bar will match foo/lib.rs but not ./foo/lib.rs.

--extern NAME=PATH
   Specify where an external rust library is located. These should match extern declarations in the crate's source code.
--sysroot PATH
   Override the system root.
-Z FLAG
   Set unstable / perma-unstable options. Use -Z help to print available options.
--color auto|always|never
   Configure coloring of output:
   auto
      colorize, if output goes to a tty (default);
   always
      always colorize output;
   never
      never colorize output.

rustc 命令实例:


rustc 编译二进制包:

rustc path/to/main.rs

rustc 使用优化进行编译(s 表示针对二进制大小进行优化;z 相同,但具有更多优化):

rustc -C lto -C opt-level=0|1|2|3|s|z path/to/main.rs

rustc 编译时使用调试信息:

rustc -g path/to/main.rs

rustc 展示解读错误信息:

rustc --explain error_code

rustc 使用针对当前 CPU 的架构特定优化进行编译:

rustc -C target-cpu=native path/to/main.rs

rustc 命令扩展阅读:


 

CommandNotFound ⚡️ 坑否 - 其他频道扩展阅读:




rustc 命令评论