]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc/src/command-line-arguments.md
New upstream version 1.41.1+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 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 <a id="option-a-allow"></a>
219 ## `-A`: set lint allowed
220
221 This flag will set which lints should be set to the [allow level](lints/levels.md#allow).
222
223 <a id="option-d-deny"></a>
224 ## `-D`: set lint denied
225
226 This flag will set which lints should be set to the [deny level](lints/levels.md#deny).
227
228 <a id="option-f-forbid"></a>
229 ## `-F`: set lint forbidden
230
231 This flag will set which lints should be set to the [forbid level](lints/levels.md#forbid).
232
233 <a id="option-z-unstable"></a>
234 ## `-Z`: set unstable options
235
236 This flag will allow you to set unstable options of rustc. In order to set multiple options,
237 the -Z flag can be used multiple times. For example: `rustc -Z verbose -Z time`.
238 Specifying options with -Z is only available on nightly. To view all available options
239 run: `rustc -Z help`.
240
241 <a id="option-cap-lints"></a>
242 ## `--cap-lints`: set the most restrictive lint level
243
244 This flag lets you 'cap' lints, for more, [see here](lints/levels.md#capping-lints).
245
246 <a id="option-codegen"></a>
247 ## `-C`/`--codegen`: code generation options
248
249 This flag will allow you to set [codegen options](codegen-options/index.md).
250
251 <a id="option-version"></a>
252 ## `-V`/`--version`: print a version
253
254 This flag will print out `rustc`'s version.
255
256 <a id="option-verbose"></a>
257 ## `-v`/`--verbose`: use verbose output
258
259 This flag, when combined with other flags, makes them produce extra output.
260
261 <a id="option-extern"></a>
262 ## `--extern`: specify where an external library is located
263
264 This flag allows you to pass the name and location for an external crate of a
265 direct dependency. Indirect dependencies (dependencies of dependencies) are
266 located using the [`-L` flag](#option-l-search-path). The given crate name is
267 added to the [extern prelude], which is the same as specifying `extern crate`
268 within the root module. The given crate name does not need to match the name
269 the library was built with.
270
271 This flag may be specified multiple times. This flag takes an argument with
272 either of the following formats:
273
274 * `CRATENAME=PATH` — Indicates the given crate is found at the given path.
275 * `CRATENAME` — Indicates the given crate may be found in the search path,
276 such as within the sysroot or via the `-L` flag.
277
278 The same crate name may be specified multiple times for different crate types.
279 If both an `rlib` and `dylib` are found, an internal algorithm is used to
280 decide which to use for linking. The [`-C prefer-dynamic`
281 flag][prefer-dynamic] may be used to influence which is used.
282
283 If the same crate name is specified with and without a path, the one with the
284 path is used and the pathless flag has no effect.
285
286 [extern prelude]: ../reference/items/extern-crates.html#extern-prelude
287 [prefer-dynamic]: codegen-options/index.md#prefer-dynamic
288
289 <a id="option-sysroot"></a>
290 ## `--sysroot`: Override the system root
291
292 The "sysroot" is where `rustc` looks for the crates that come with the Rust
293 distribution; this flag allows that to be overridden.
294
295 <a id="option-error-format"></a>
296 ## `--error-format`: control how errors are produced
297
298 This flag lets you control the format of messages. Messages are printed to
299 stderr. The valid options are:
300
301 - `human` — Human-readable output. This is the default.
302 - `json` — Structured JSON output. See [the JSON chapter] for more detail.
303 - `short` — Short, one-line messages.
304
305 <a id="option-color"></a>
306 ## `--color`: configure coloring of output
307
308 This flag lets you control color settings of the output. The valid options
309 are:
310
311 - `auto` — Use colors if output goes to a tty. This is the default.
312 - `always` — Always use colors.
313 - `never` — Never colorize output.
314
315 <a id="option-remap-path-prefix"></a>
316 ## `--remap-path-prefix`: remap source names in output
317
318 Remap source path prefixes in all output, including compiler diagnostics,
319 debug information, macro expansions, etc. It takes a value of the form
320 `FROM=TO` where a path prefix equal to `FROM` is rewritten to the value `TO`.
321 The `FROM` may itself contain an `=` symbol, but the `TO` value may not. This
322 flag may be specified multiple times.
323
324 This is useful for normalizing build products, for example by removing the
325 current directory out of pathnames emitted into the object files. The
326 replacement is purely textual, with no consideration of the current system's
327 pathname syntax. For example `--remap-path-prefix foo=bar` will match
328 `foo/lib.rs` but not `./foo/lib.rs`.
329
330 <a id="option-json"></a>
331 ## `--json`: configure json messages printed by the compiler
332
333 When the [`--error-format=json` option](#option-error-format) is passed to
334 rustc then all of the compiler's diagnostic output will be emitted in the form
335 of JSON blobs. The `--json` argument can be used in conjunction with
336 `--error-format=json` to configure what the JSON blobs contain as well as
337 which ones are emitted.
338
339 With `--error-format=json` the compiler will always emit any compiler errors as
340 a JSON blob, but the following options are also available to the `--json` flag
341 to customize the output:
342
343 - `diagnostic-short` - json blobs for diagnostic messages should use the "short"
344 rendering instead of the normal "human" default. This means that the output of
345 `--error-format=short` will be embedded into the JSON diagnostics instead of
346 the default `--error-format=human`.
347
348 - `diagnostic-rendered-ansi` - by default JSON blobs in their `rendered` field
349 will contain a plain text rendering of the diagnostic. This option instead
350 indicates that the diagnostic should have embedded ANSI color codes intended
351 to be used to colorize the message in the manner rustc typically already does
352 for terminal outputs. Note that this is usefully combined with crates like
353 [`fwdansi`](https://crates.io/crates/fwdansi) to translate these ANSI codes
354 on Windows to console commands or
355 [`strip-ansi-escapes`](https://crates.io/crates/strip-ansi-escapes) if you'd
356 like to optionally remove the ansi colors afterwards.
357
358 - `artifacts` - this instructs rustc to emit a JSON blob for each artifact that
359 is emitted. An artifact corresponds to a request from the [`--emit` CLI
360 argument](#option-emit), and as soon as the artifact is available on the
361 filesystem a notification will be emitted.
362
363 Note that it is invalid to combine the `--json` argument with the
364 [`--color`](#option-color) argument, and it is required to combine `--json`
365 with `--error-format=json`.
366
367 See [the JSON chapter] for more detail.
368
369 <a id="at-path"></a>
370 ## `@path`: load command-line flags from a path
371
372 If you specify `@path` on the command-line, then it will open `path` and read
373 command line options from it. These options are one per line; a blank line indicates
374 an empty option. The file can use Unix or Windows style line endings, and must be
375 encoded as UTF-8.
376
377 [the JSON chapter]: json.md