]> git.lizzy.rs Git - rust.git/blob - src/doc/rustc/src/command-line-arguments.md
Update const_forget.rs
[rust.git] / src / doc / rustc / src / command-line-arguments.md
1 # Command-line arguments
2
3 Here's a list of command-line arguments to `rustc` and what they do.
4
5 <a id="option-help"></a>
6 ## `-h`/`--help`: get help
7
8 This flag will print out help information for `rustc`.
9
10 <a id="option-cfg"></a>
11 ## `--cfg`: configure the compilation environment
12
13 This flag can turn on or off various `#[cfg]` settings for [conditional
14 compilation](../reference/conditional-compilation.md).
15
16 The value can either be a single identifier or two identifiers separated by `=`.
17
18 For examples, `--cfg 'verbose'` or `--cfg 'feature="serde"'`. These correspond
19 to `#[cfg(verbose)]` and `#[cfg(feature = "serde")]` respectively.
20
21 <a id="option-l-search-path"></a>
22 ## `-L`: add a directory to the library search path
23
24 The `-L` flag adds a path to search for external crates and libraries.
25
26 The kind of search path can optionally be specified with the form `-L
27 KIND=PATH` where `KIND` may be one of:
28
29 - `dependency` — Only search for transitive dependencies in this directory.
30 - `crate` — Only search for this crate's direct dependencies in this
31   directory.
32 - `native` — Only search for native libraries in this directory.
33 - `framework` — Only search for macOS frameworks in this directory.
34 - `all` — Search for all library kinds in this directory. This is the default
35   if `KIND` is not specified.
36
37 <a id="option-l-link-lib"></a>
38 ## `-l`: link the generated crate to a native library
39
40 This flag allows you to specify linking to a specific native library when building
41 a crate.
42
43 The kind of library can optionally be specified with the form `-l KIND=lib`
44 where `KIND` may be one of:
45
46 - `dylib` — A native dynamic library.
47 - `static` — A native static library (such as a `.a` archive).
48 - `framework` — A macOS framework.
49
50 The kind of library can be specified in a [`#[link]`
51 attribute][link-attribute]. If the kind is not specified in the `link`
52 attribute or on the command-line, it will link a dynamic library if available,
53 otherwise it will use a static library. If the kind is specified on the
54 command-line, it will override the kind specified in a `link` attribute.
55
56 The name used in a `link` attribute may be overridden using the form `-l
57 ATTR_NAME:LINK_NAME` where `ATTR_NAME` is the name in the `link` attribute,
58 and `LINK_NAME` is the name of the actual library that will be linked.
59
60 [link-attribute]: ../reference/items/external-blocks.html#the-link-attribute
61
62 <a id="option-crate-type"></a>
63 ## `--crate-type`: a list of types of crates for the compiler to emit
64
65 This instructs `rustc` on which crate type to build. This flag accepts a
66 comma-separated list of values, and may be specified multiple times. The valid
67 crate types are:
68
69 - `lib` — Generates a library kind preferred by the compiler, currently
70   defaults to `rlib`.
71 - `rlib` — A Rust static library.
72 - `staticlib` — A native static library.
73 - `dylib` — A Rust dynamic library.
74 - `cdylib` — A native dynamic library.
75 - `bin` — A runnable executable program.
76 - `proc-macro` — Generates a format suitable for a procedural macro library
77   that may be loaded by the compiler.
78
79 The crate type may be specified with the [`crate_type` attribute][crate_type].
80 The `--crate-type` command-line value will override the `crate_type`
81 attribute.
82
83 More details may be found in the [linkage chapter] of the reference.
84
85 [linkage chapter]: ../reference/linkage.html
86 [crate_type]: ../reference/linkage.html
87
88 <a id="option-crate-name"></a>
89 ## `--crate-name`: specify the name of the crate being built
90
91 This informs `rustc` of the name of your crate.
92
93 <a id="option-edition"></a>
94 ## `--edition`: specify the edition to use
95
96 This flag takes a value of `2015` or `2018`. The default is `2015`. More
97 information about editions may be found in the [edition guide].
98
99 [edition guide]: ../edition-guide/introduction.html
100
101 <a id="option-emit"></a>
102 ## `--emit`: specifies the types of output files to generate
103
104 This flag controls the types of output files generated by the compiler. It
105 accepts a comma-separated list of values, and may be specified multiple times.
106 The valid emit kinds are:
107
108 - `asm` — Generates a file with the crate's assembly code. The default output
109   filename is `CRATE_NAME.s`.
110 - `dep-info` — Generates a file with Makefile syntax that indicates all the
111   source files that were loaded to generate the crate. The default output
112   filename is `CRATE_NAME.d`.
113 - `link` — Generates the crates specified by `--crate-type`. The default
114   output filenames depend on the crate type and platform. This is the default
115   if `--emit` is not specified.
116 - `llvm-bc` — Generates a binary file containing the [LLVM bitcode]. The
117   default output filename is `CRATE_NAME.bc`.
118 - `llvm-ir` — Generates a file containing [LLVM IR]. The default output
119   filename is `CRATE_NAME.ll`.
120 - `metadata` — Generates a file containing metadata about the crate. The
121   default output filename is `CRATE_NAME.rmeta`.
122 - `mir` — Generates a file containing rustc's mid-level intermediate
123   representation. The default output filename is `CRATE_NAME.mir`.
124 - `obj` — Generates a native object file. The default output filename is
125   `CRATE_NAME.o`.
126
127 The output filename can be set with the [`-o` flag](#option-o-output). A
128 suffix may be added to the filename with the [`-C extra-filename`
129 flag](codegen-options/index.md#extra-filename). The files are written to the
130 current directory unless the [`--out-dir` flag](#option-out-dir) is used. Each
131 emission type may also specify the output filename with the form `KIND=PATH`,
132 which takes precedence over the `-o` flag.
133
134 [LLVM bitcode]: https://llvm.org/docs/BitCodeFormat.html
135 [LLVM IR]: https://llvm.org/docs/LangRef.html
136
137 <a id="option-print"></a>
138 ## `--print`: print compiler information
139
140 This flag prints out various information about the compiler. This flag may be
141 specified multiple times, and the information is printed in the order the
142 flags are specified. Specifying a `--print` flag will usually disable the
143 [`--emit`](#option-emit) step and will only print the requested information.
144 The valid types of print values are:
145
146 - `crate-name` — The name of the crate.
147 - `file-names` — The names of the files created by the `link` emit kind.
148 - `sysroot` — Path to the sysroot.
149 - `cfg` — List of cfg values. See [conditional compilation] for more
150   information about cfg values.
151 - `target-list` — List of known targets. The target may be selected with the
152   `--target` flag.
153 - `target-cpus` — List of available CPU values for the current target. The
154   target CPU may be selected with the [`-C target-cpu=val`
155   flag](codegen-options/index.md#target-cpu).
156 - `target-features` — List of available target features for the current
157   target. Target features may be enabled with the [`-C target-feature=val`
158   flag](codegen-options/index.md#target-feature).  This flag is unsafe. See
159   [known issues](targets/known-issues.md) for more details.
160 - `relocation-models` — List of relocation models. Relocation models may be
161   selected with the [`-C relocation-model=val`
162   flag](codegen-options/index.md#relocation-model).
163 - `code-models` — List of code models. Code models may be selected with the
164   [`-C code-model=val` flag](codegen-options/index.md#code-model).
165 - `tls-models` — List of Thread Local Storage models supported. The model may
166   be selected with the `-Z tls-model=val` flag.
167 - `native-static-libs` — This may be used when creating a `staticlib` crate
168   type. If this is the only flag, it will perform a full compilation and
169   include a diagnostic note that indicates the linker flags to use when
170   linking the resulting static library. The note starts with the text
171   `native-static-libs:` to make it easier to fetch the output.
172
173 [conditional compilation]: ../reference/conditional-compilation.html
174
175 <a id="option-g-debug"></a>
176 ## `-g`: include debug information
177
178 A synonym for [`-C debuginfo=2`](codegen-options/index.md#debuginfo).
179
180 <a id="option-o-optimize"></a>
181 ## `-O`: optimize your code
182
183 A synonym for [`-C opt-level=2`](codegen-options/index.md#opt-level).
184
185 <a id="option-o-output"></a>
186 ## `-o`: filename of the output
187
188 This flag controls the output filename.
189
190 <a id="option-out-dir"></a>
191 ## `--out-dir`: directory to write the output in
192
193 The outputted crate will be written to this directory. This flag is ignored if
194 the [`-o` flag](#option-o-output) is used.
195
196 <a id="option-explain"></a>
197 ## `--explain`: provide a detailed explanation of an error message
198
199 Each error of `rustc`'s comes with an error code; this will print
200 out a longer explanation of a given error.
201
202 <a id="option-test"></a>
203 ## `--test`: build a test harness
204
205 When compiling this crate, `rustc` will ignore your `main` function
206 and instead produce a test harness.
207
208 <a id="option-target"></a>
209 ## `--target`: select a target triple to build
210
211 This controls which [target](targets/index.md) to produce.
212
213 <a id="option-w-warn"></a>
214 ## `-W`: set lint warnings
215
216 This flag will set which lints should be set to the [warn level](lints/levels.md#warn).
217
218 _Note:_ The order of these lint level arguments is taken into account, see [lint level via compiler flag](lints/levels.md#via-compiler-flag) for more information.
219
220 <a id="option-a-allow"></a>
221 ## `-A`: set lint allowed
222
223 This flag will set which lints should be set to the [allow level](lints/levels.md#allow).
224
225 _Note:_ The order of these lint level arguments is taken into account, see [lint level via compiler flag](lints/levels.md#via-compiler-flag) for more information.
226
227 <a id="option-d-deny"></a>
228 ## `-D`: set lint denied
229
230 This flag will set which lints should be set to the [deny level](lints/levels.md#deny).
231
232 _Note:_ The order of these lint level arguments is taken into account, see [lint level via compiler flag](lints/levels.md#via-compiler-flag) for more information.
233
234 <a id="option-f-forbid"></a>
235 ## `-F`: set lint forbidden
236
237 This flag will set which lints should be set to the [forbid level](lints/levels.md#forbid).
238
239 _Note:_ The order of these lint level arguments is taken into account, see [lint level via compiler flag](lints/levels.md#via-compiler-flag) for more information.
240
241 <a id="option-z-unstable"></a>
242 ## `-Z`: set unstable options
243
244 This flag will allow you to set unstable options of rustc. In order to set multiple options,
245 the -Z flag can be used multiple times. For example: `rustc -Z verbose -Z time`.
246 Specifying options with -Z is only available on nightly. To view all available options
247 run: `rustc -Z help`.
248
249 <a id="option-cap-lints"></a>
250 ## `--cap-lints`: set the most restrictive lint level
251
252 This flag lets you 'cap' lints, for more, [see here](lints/levels.md#capping-lints).
253
254 <a id="option-codegen"></a>
255 ## `-C`/`--codegen`: code generation options
256
257 This flag will allow you to set [codegen options](codegen-options/index.md).
258
259 <a id="option-version"></a>
260 ## `-V`/`--version`: print a version
261
262 This flag will print out `rustc`'s version.
263
264 <a id="option-verbose"></a>
265 ## `-v`/`--verbose`: use verbose output
266
267 This flag, when combined with other flags, makes them produce extra output.
268
269 <a id="option-extern"></a>
270 ## `--extern`: specify where an external library is located
271
272 This flag allows you to pass the name and location for an external crate of a
273 direct dependency. Indirect dependencies (dependencies of dependencies) are
274 located using the [`-L` flag](#option-l-search-path). The given crate name is
275 added to the [extern prelude], which is the same as specifying `extern crate`
276 within the root module. The given crate name does not need to match the name
277 the library was built with.
278
279 This flag may be specified multiple times. This flag takes an argument with
280 either of the following formats:
281
282 * `CRATENAME=PATH` — Indicates the given crate is found at the given path.
283 * `CRATENAME` — Indicates the given crate may be found in the search path,
284   such as within the sysroot or via the `-L` flag.
285
286 The same crate name may be specified multiple times for different crate types.
287 If both an `rlib` and `dylib` are found, an internal algorithm is used to
288 decide which to use for linking. The [`-C prefer-dynamic`
289 flag][prefer-dynamic] may be used to influence which is used.
290
291 If the same crate name is specified with and without a path, the one with the
292 path is used and the pathless flag has no effect.
293
294 [extern prelude]: ../reference/items/extern-crates.html#extern-prelude
295 [prefer-dynamic]: codegen-options/index.md#prefer-dynamic
296
297 <a id="option-sysroot"></a>
298 ## `--sysroot`: Override the system root
299
300 The "sysroot" is where `rustc` looks for the crates that come with the Rust
301 distribution; this flag allows that to be overridden.
302
303 <a id="option-error-format"></a>
304 ## `--error-format`: control how errors are produced
305
306 This flag lets you control the format of messages. Messages are printed to
307 stderr. The valid options are:
308
309 - `human` — Human-readable output. This is the default.
310 - `json` — Structured JSON output. See [the JSON chapter] for more detail.
311 - `short` — Short, one-line messages.
312
313 <a id="option-color"></a>
314 ## `--color`: configure coloring of output
315
316 This flag lets you control color settings of the output. The valid options
317 are:
318
319 - `auto` — Use colors if output goes to a tty. This is the default.
320 - `always` — Always use colors.
321 - `never` — Never colorize output.
322
323 <a id="option-remap-path-prefix"></a>
324 ## `--remap-path-prefix`: remap source names in output
325
326 Remap source path prefixes in all output, including compiler diagnostics,
327 debug information, macro expansions, etc. It takes a value of the form
328 `FROM=TO` where a path prefix equal to `FROM` is rewritten to the value `TO`.
329 The `FROM` may itself contain an `=` symbol, but the `TO` value may not. This
330 flag may be specified multiple times.
331
332 This is useful for normalizing build products, for example by removing the
333 current directory out of pathnames emitted into the object files. The
334 replacement is purely textual, with no consideration of the current system's
335 pathname syntax. For example `--remap-path-prefix foo=bar` will match
336 `foo/lib.rs` but not `./foo/lib.rs`.
337
338 <a id="option-json"></a>
339 ## `--json`: configure json messages printed by the compiler
340
341 When the [`--error-format=json` option](#option-error-format) is passed to
342 rustc then all of the compiler's diagnostic output will be emitted in the form
343 of JSON blobs. The `--json` argument can be used in conjunction with
344 `--error-format=json` to configure what the JSON blobs contain as well as
345 which ones are emitted.
346
347 With `--error-format=json` the compiler will always emit any compiler errors as
348 a JSON blob, but the following options are also available to the `--json` flag
349 to customize the output:
350
351 - `diagnostic-short` - json blobs for diagnostic messages should use the "short"
352   rendering instead of the normal "human" default. This means that the output of
353   `--error-format=short` will be embedded into the JSON diagnostics instead of
354   the default `--error-format=human`.
355
356 - `diagnostic-rendered-ansi` - by default JSON blobs in their `rendered` field
357   will contain a plain text rendering of the diagnostic. This option instead
358   indicates that the diagnostic should have embedded ANSI color codes intended
359   to be used to colorize the message in the manner rustc typically already does
360   for terminal outputs. Note that this is usefully combined with crates like
361   [`fwdansi`](https://crates.io/crates/fwdansi) to translate these ANSI codes
362   on Windows to console commands or
363   [`strip-ansi-escapes`](https://crates.io/crates/strip-ansi-escapes) if you'd
364   like to optionally remove the ansi colors afterwards.
365
366 - `artifacts` - this instructs rustc to emit a JSON blob for each artifact that
367   is emitted. An artifact corresponds to a request from the [`--emit` CLI
368   argument](#option-emit), and as soon as the artifact is available on the
369   filesystem a notification will be emitted.
370
371 Note that it is invalid to combine the `--json` argument with the
372 [`--color`](#option-color) argument, and it is required to combine `--json`
373 with `--error-format=json`.
374
375 See [the JSON chapter] for more detail.
376
377 <a id="at-path"></a>
378 ## `@path`: load command-line flags from a path
379
380 If you specify `@path` on the command-line, then it will open `path` and read
381 command line options from it. These options are one per line; a blank line indicates
382 an empty option. The file can use Unix or Windows style line endings, and must be
383 encoded as UTF-8.
384
385 [the JSON chapter]: json.md