]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc/src/command-line-arguments.md
New upstream version 1.68.2+dfsg1
[rustc.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 Syntax: `-l [KIND[:MODIFIERS]=]NAME[:RENAME]`.
41
42 This flag allows you to specify linking to a specific native library when building
43 a crate.
44
45 The kind of library can optionally be specified with the form `-l KIND=lib`
46 where `KIND` may be one of:
47
48 - `dylib` — A native dynamic library.
49 - `static` — A native static library (such as a `.a` archive).
50 - `framework` — A macOS framework.
51
52 If the kind is specified, then linking modifiers can be attached to it.
53 Modifiers are specified as a comma-delimited string with each modifier prefixed with
54 either a `+` or `-` to indicate that the modifier is enabled or disabled, respectively.
55 Specifying multiple `modifiers` arguments in a single `link` attribute,
56 or multiple identical modifiers in the same `modifiers` argument is not currently supported. \
57 Example: `-l static:+whole-archive=mylib`.
58
59 The kind of library and the modifiers can also be specified in a [`#[link]`
60 attribute][link-attribute]. If the kind is not specified in the `link`
61 attribute or on the command-line, it will link a dynamic library if available,
62 otherwise it will use a static library. If the kind is specified on the
63 command-line, it will override the kind specified in a `link` attribute.
64
65 The name used in a `link` attribute may be overridden using the form `-l
66 ATTR_NAME:LINK_NAME` where `ATTR_NAME` is the name in the `link` attribute,
67 and `LINK_NAME` is the name of the actual library that will be linked.
68
69 [link-attribute]: ../reference/items/external-blocks.html#the-link-attribute
70
71 ### Linking modifiers: `whole-archive`
72
73 This modifier is only compatible with the `static` linking kind.
74 Using any other kind will result in a compiler error.
75
76 `+whole-archive` means that the static library is linked as a whole archive
77 without throwing any object files away.
78
79 This modifier translates to `--whole-archive` for `ld`-like linkers,
80 to `/WHOLEARCHIVE` for `link.exe`, and to `-force_load` for `ld64`.
81 The modifier does nothing for linkers that don't support it.
82
83 The default for this modifier is `-whole-archive`. \
84 NOTE: The default may currently be different in some cases for backward compatibility,
85 but it is not guaranteed. If you need whole archive semantics use `+whole-archive` explicitly.
86
87 ### Linking modifiers: `bundle`
88
89 This modifier is only compatible with the `static` linking kind.
90 Using any other kind will result in a compiler error.
91
92 When building a rlib or staticlib `+bundle` means that the native static library
93 will be packed into the rlib or staticlib archive, and then retrieved from there
94 during linking of the final binary.
95
96 When building a rlib `-bundle` means that the native static library is registered as a dependency
97 of that rlib "by name", and object files from it are included only during linking of the final
98 binary, the file search by that name is also performed during final linking. \
99 When building a staticlib `-bundle` means that the native static library is simply not included
100 into the archive and some higher level build system will need to add it later during linking of
101 the final binary.
102
103 This modifier has no effect when building other targets like executables or dynamic libraries.
104
105 The default for this modifier is `+bundle`.
106
107 ### Linking modifiers: `verbatim`
108
109 This modifier is compatible with all linking kinds.
110
111 `+verbatim` means that rustc itself won't add any target-specified library prefixes or suffixes
112 (like `lib` or `.a`) to the library name, and will try its best to ask for the same thing from the
113 linker.
114
115 For `ld`-like linkers supporting GNU extensions rustc will use the `-l:filename` syntax (note the
116 colon) when passing the library, so the linker won't add any prefixes or suffixes to it.
117 See [`-l namespec`](https://sourceware.org/binutils/docs/ld/Options.html) in ld documentation for
118 more details. \
119 For linkers not supporting any verbatim modifiers (e.g. `link.exe` or `ld64`) the library name will
120 be passed as is. So the most reliable cross-platform use scenarios for this option are when no
121 linker is involved, for example bundling native libraries into rlibs.
122
123 `-verbatim` means that rustc will either add a target-specific prefix and suffix to the library
124 name before passing it to linker, or won't prevent linker from implicitly adding it. \
125 In case of `raw-dylib` kind in particular `.dll` will be added to the library name on Windows.
126
127 The default for this modifier is `-verbatim`.
128
129 NOTE: Even with `+verbatim` and `-l:filename` syntax `ld`-like linkers do not typically support
130 passing absolute paths to libraries. Usually such paths need to be passed as input files without
131 using any options like `-l`, e.g. `ld /my/absolute/path`. \
132 `-Clink-arg=/my/absolute/path` can be used for doing this from stable `rustc`.
133
134 <a id="option-crate-type"></a>
135 ## `--crate-type`: a list of types of crates for the compiler to emit
136
137 This instructs `rustc` on which crate type to build. This flag accepts a
138 comma-separated list of values, and may be specified multiple times. The valid
139 crate types are:
140
141 - `lib` — Generates a library kind preferred by the compiler, currently
142 defaults to `rlib`.
143 - `rlib` — A Rust static library.
144 - `staticlib` — A native static library.
145 - `dylib` — A Rust dynamic library.
146 - `cdylib` — A native dynamic library.
147 - `bin` — A runnable executable program.
148 - `proc-macro` — Generates a format suitable for a procedural macro library
149 that may be loaded by the compiler.
150
151 The crate type may be specified with the [`crate_type` attribute][crate_type].
152 The `--crate-type` command-line value will override the `crate_type`
153 attribute.
154
155 More details may be found in the [linkage chapter] of the reference.
156
157 [linkage chapter]: ../reference/linkage.html
158 [crate_type]: ../reference/linkage.html
159
160 <a id="option-crate-name"></a>
161 ## `--crate-name`: specify the name of the crate being built
162
163 This informs `rustc` of the name of your crate.
164
165 <a id="option-edition"></a>
166 ## `--edition`: specify the edition to use
167
168 This flag takes a value of `2015`, `2018` or `2021`. The default is `2015`. More
169 information about editions may be found in the [edition guide].
170
171 [edition guide]: ../edition-guide/introduction.html
172
173 <a id="option-emit"></a>
174 ## `--emit`: specifies the types of output files to generate
175
176 This flag controls the types of output files generated by the compiler. It
177 accepts a comma-separated list of values, and may be specified multiple times.
178 The valid emit kinds are:
179
180 - `asm` — Generates a file with the crate's assembly code. The default output
181 filename is `CRATE_NAME.s`.
182 - `dep-info` — Generates a file with Makefile syntax that indicates all the
183 source files that were loaded to generate the crate. The default output
184 filename is `CRATE_NAME.d`.
185 - `link` — Generates the crates specified by `--crate-type`. The default
186 output filenames depend on the crate type and platform. This is the default
187 if `--emit` is not specified.
188 - `llvm-bc` — Generates a binary file containing the [LLVM bitcode]. The
189 default output filename is `CRATE_NAME.bc`.
190 - `llvm-ir` — Generates a file containing [LLVM IR]. The default output
191 filename is `CRATE_NAME.ll`.
192 - `metadata` — Generates a file containing metadata about the crate. The
193 default output filename is `libCRATE_NAME.rmeta`.
194 - `mir` — Generates a file containing rustc's mid-level intermediate
195 representation. The default output filename is `CRATE_NAME.mir`.
196 - `obj` — Generates a native object file. The default output filename is
197 `CRATE_NAME.o`.
198
199 The output filename can be set with the [`-o` flag](#option-o-output). A
200 suffix may be added to the filename with the [`-C extra-filename`
201 flag](codegen-options/index.md#extra-filename). The files are written to the
202 current directory unless the [`--out-dir` flag](#option-out-dir) is used. Each
203 emission type may also specify the output filename with the form `KIND=PATH`,
204 which takes precedence over the `-o` flag.
205
206 [LLVM bitcode]: https://llvm.org/docs/BitCodeFormat.html
207 [LLVM IR]: https://llvm.org/docs/LangRef.html
208
209 <a id="option-print"></a>
210 ## `--print`: print compiler information
211
212 This flag prints out various information about the compiler. This flag may be
213 specified multiple times, and the information is printed in the order the
214 flags are specified. Specifying a `--print` flag will usually disable the
215 [`--emit`](#option-emit) step and will only print the requested information.
216 The valid types of print values are:
217
218 - `crate-name` — The name of the crate.
219 - `file-names` — The names of the files created by the `link` emit kind.
220 - `sysroot` — Path to the sysroot.
221 - `target-libdir` - Path to the target libdir.
222 - `cfg` — List of cfg values. See [conditional compilation] for more
223 information about cfg values.
224 - `target-list` — List of known targets. The target may be selected with the
225 `--target` flag.
226 - `target-cpus` — List of available CPU values for the current target. The
227 target CPU may be selected with the [`-C target-cpu=val`
228 flag](codegen-options/index.md#target-cpu).
229 - `target-features` — List of available target features for the current
230 target. Target features may be enabled with the [`-C target-feature=val`
231 flag](codegen-options/index.md#target-feature). This flag is unsafe. See
232 [known issues](targets/known-issues.md) for more details.
233 - `relocation-models` — List of relocation models. Relocation models may be
234 selected with the [`-C relocation-model=val`
235 flag](codegen-options/index.md#relocation-model).
236 - `code-models` — List of code models. Code models may be selected with the
237 [`-C code-model=val` flag](codegen-options/index.md#code-model).
238 - `tls-models` — List of Thread Local Storage models supported. The model may
239 be selected with the `-Z tls-model=val` flag.
240 - `native-static-libs` — This may be used when creating a `staticlib` crate
241 type. If this is the only flag, it will perform a full compilation and
242 include a diagnostic note that indicates the linker flags to use when
243 linking the resulting static library. The note starts with the text
244 `native-static-libs:` to make it easier to fetch the output.
245 - `link-args` — This flag does not disable the `--emit` step. When linking,
246 this flag causes `rustc` to print the full linker invocation in a
247 human-readable form. This can be useful when debugging linker options. The
248 exact format of this debugging output is not a stable guarantee, other than
249 that it will include the linker executable and the text of each command-line
250 argument passed to the linker.
251
252 [conditional compilation]: ../reference/conditional-compilation.html
253
254 <a id="option-g-debug"></a>
255 ## `-g`: include debug information
256
257 A synonym for [`-C debuginfo=2`](codegen-options/index.md#debuginfo).
258
259 <a id="option-o-optimize"></a>
260 ## `-O`: optimize your code
261
262 A synonym for [`-C opt-level=2`](codegen-options/index.md#opt-level).
263
264 <a id="option-o-output"></a>
265 ## `-o`: filename of the output
266
267 This flag controls the output filename.
268
269 <a id="option-out-dir"></a>
270 ## `--out-dir`: directory to write the output in
271
272 The outputted crate will be written to this directory. This flag is ignored if
273 the [`-o` flag](#option-o-output) is used.
274
275 <a id="option-explain"></a>
276 ## `--explain`: provide a detailed explanation of an error message
277
278 Each error of `rustc`'s comes with an error code; this will print
279 out a longer explanation of a given error.
280
281 <a id="option-test"></a>
282 ## `--test`: build a test harness
283
284 When compiling this crate, `rustc` will ignore your `main` function
285 and instead produce a test harness. See the [Tests chapter](tests/index.md)
286 for more information about tests.
287
288 <a id="option-target"></a>
289 ## `--target`: select a target triple to build
290
291 This controls which [target](targets/index.md) to produce.
292
293 <a id="option-w-warn"></a>
294 ## `-W`: set lint warnings
295
296 This flag will set which lints should be set to the [warn level](lints/levels.md#warn).
297
298 _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.
299
300 <a id="option-force-warn"></a>
301 ## `--force-warn`: force a lint to warn
302
303 This flag sets the given lint to the [forced warn level](lints/levels.md#force-warn) and the level cannot be overridden, even ignoring the [lint caps](lints/levels.md#capping-lints).
304
305 <a id="option-a-allow"></a>
306 ## `-A`: set lint allowed
307
308 This flag will set which lints should be set to the [allow level](lints/levels.md#allow).
309
310 _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.
311
312 <a id="option-d-deny"></a>
313 ## `-D`: set lint denied
314
315 This flag will set which lints should be set to the [deny level](lints/levels.md#deny).
316
317 _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.
318
319 <a id="option-f-forbid"></a>
320 ## `-F`: set lint forbidden
321
322 This flag will set which lints should be set to the [forbid level](lints/levels.md#forbid).
323
324 _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.
325
326 <a id="option-z-unstable"></a>
327 ## `-Z`: set unstable options
328
329 This flag will allow you to set unstable options of rustc. In order to set multiple options,
330 the -Z flag can be used multiple times. For example: `rustc -Z verbose -Z time-passes`.
331 Specifying options with -Z is only available on nightly. To view all available options
332 run: `rustc -Z help`, or see [The Unstable Book](../unstable-book/index.html).
333
334 <a id="option-cap-lints"></a>
335 ## `--cap-lints`: set the most restrictive lint level
336
337 This flag lets you 'cap' lints, for more, [see here](lints/levels.md#capping-lints).
338
339 <a id="option-codegen"></a>
340 ## `-C`/`--codegen`: code generation options
341
342 This flag will allow you to set [codegen options](codegen-options/index.md).
343
344 <a id="option-version"></a>
345 ## `-V`/`--version`: print a version
346
347 This flag will print out `rustc`'s version.
348
349 <a id="option-verbose"></a>
350 ## `-v`/`--verbose`: use verbose output
351
352 This flag, when combined with other flags, makes them produce extra output.
353
354 <a id="option-extern"></a>
355 ## `--extern`: specify where an external library is located
356
357 This flag allows you to pass the name and location for an external crate of a
358 direct dependency. Indirect dependencies (dependencies of dependencies) are
359 located using the [`-L` flag](#option-l-search-path). The given crate name is
360 added to the [extern prelude], similar to specifying `extern crate` within the
361 root module. The given crate name does not need to match the name
362 the library was built with.
363
364 Specifying `--extern` has one behavior difference from `extern crate`:
365 `--extern` merely makes the crate a _candidate_ for being linked; it does not
366 actually link it unless it's actively used. In rare occasions you may wish
367 to ensure a crate is linked even if you don't actively use it from your
368 code: for example, if it changes the global allocator or if it contains
369 `#[no_mangle]` symbols for use by other programming languages. In such
370 cases you'll need to use `extern crate`.
371
372 This flag may be specified multiple times. This flag takes an argument with
373 either of the following formats:
374
375 * `CRATENAME=PATH` — Indicates the given crate is found at the given path.
376 * `CRATENAME` — Indicates the given crate may be found in the search path,
377 such as within the sysroot or via the `-L` flag.
378
379 The same crate name may be specified multiple times for different crate types.
380 If both an `rlib` and `dylib` are found, an internal algorithm is used to
381 decide which to use for linking. The [`-C prefer-dynamic`
382 flag][prefer-dynamic] may be used to influence which is used.
383
384 If the same crate name is specified with and without a path, the one with the
385 path is used and the pathless flag has no effect.
386
387 [extern prelude]: ../reference/names/preludes.html#extern-prelude
388 [prefer-dynamic]: codegen-options/index.md#prefer-dynamic
389
390 <a id="option-sysroot"></a>
391 ## `--sysroot`: Override the system root
392
393 The "sysroot" is where `rustc` looks for the crates that come with the Rust
394 distribution; this flag allows that to be overridden.
395
396 <a id="option-error-format"></a>
397 ## `--error-format`: control how errors are produced
398
399 This flag lets you control the format of messages. Messages are printed to
400 stderr. The valid options are:
401
402 - `human` — Human-readable output. This is the default.
403 - `json` — Structured JSON output. See [the JSON chapter] for more detail.
404 - `short` — Short, one-line messages.
405
406 <a id="option-color"></a>
407 ## `--color`: configure coloring of output
408
409 This flag lets you control color settings of the output. The valid options
410 are:
411
412 - `auto` — Use colors if output goes to a tty. This is the default.
413 - `always` — Always use colors.
414 - `never` — Never colorize output.
415
416 <a id="option-diagnostic-width"></a>
417 ## `--diagnostic-width`: specify the terminal width for diagnostics
418
419 This flag takes a number that specifies the width of the terminal in characters.
420 Formatting of diagnostics will take the width into consideration to make them better fit on the screen.
421
422 <a id="option-remap-path-prefix"></a>
423 ## `--remap-path-prefix`: remap source names in output
424
425 Remap source path prefixes in all output, including compiler diagnostics,
426 debug information, macro expansions, etc. It takes a value of the form
427 `FROM=TO` where a path prefix equal to `FROM` is rewritten to the value `TO`.
428 The `FROM` may itself contain an `=` symbol, but the `TO` value may not. This
429 flag may be specified multiple times.
430
431 This is useful for normalizing build products, for example by removing the
432 current directory out of pathnames emitted into the object files. The
433 replacement is purely textual, with no consideration of the current system's
434 pathname syntax. For example `--remap-path-prefix foo=bar` will match
435 `foo/lib.rs` but not `./foo/lib.rs`.
436
437 When multiple remappings are given and several of them match, the **last**
438 matching one is applied.
439
440 <a id="option-json"></a>
441 ## `--json`: configure json messages printed by the compiler
442
443 When the [`--error-format=json` option](#option-error-format) is passed to
444 rustc then all of the compiler's diagnostic output will be emitted in the form
445 of JSON blobs. The `--json` argument can be used in conjunction with
446 `--error-format=json` to configure what the JSON blobs contain as well as
447 which ones are emitted.
448
449 With `--error-format=json` the compiler will always emit any compiler errors as
450 a JSON blob, but the following options are also available to the `--json` flag
451 to customize the output:
452
453 - `diagnostic-short` - json blobs for diagnostic messages should use the "short"
454 rendering instead of the normal "human" default. This means that the output of
455 `--error-format=short` will be embedded into the JSON diagnostics instead of
456 the default `--error-format=human`.
457
458 - `diagnostic-rendered-ansi` - by default JSON blobs in their `rendered` field
459 will contain a plain text rendering of the diagnostic. This option instead
460 indicates that the diagnostic should have embedded ANSI color codes intended
461 to be used to colorize the message in the manner rustc typically already does
462 for terminal outputs. Note that this is usefully combined with crates like
463 [`fwdansi`](https://crates.io/crates/fwdansi) to translate these ANSI codes
464 on Windows to console commands or
465 [`strip-ansi-escapes`](https://crates.io/crates/strip-ansi-escapes) if you'd
466 like to optionally remove the ansi colors afterwards.
467
468 - `artifacts` - this instructs rustc to emit a JSON blob for each artifact that
469 is emitted. An artifact corresponds to a request from the [`--emit` CLI
470 argument](#option-emit), and as soon as the artifact is available on the
471 filesystem a notification will be emitted.
472
473 - `future-incompat` - includes a JSON message that contains a report if the
474 crate contains any code that may fail to compile in the future.
475
476 Note that it is invalid to combine the `--json` argument with the
477 [`--color`](#option-color) argument, and it is required to combine `--json`
478 with `--error-format=json`.
479
480 See [the JSON chapter] for more detail.
481
482 <a id="at-path"></a>
483 ## `@path`: load command-line flags from a path
484
485 If you specify `@path` on the command-line, then it will open `path` and read
486 command line options from it. These options are one per line; a blank line indicates
487 an empty option. The file can use Unix or Windows style line endings, and must be
488 encoded as UTF-8.
489
490 [the JSON chapter]: json.md