1CARGO-BUILD(1) General Commands Manual CARGO-BUILD(1)
2
3
4
6 cargo-build - Compile the current package
7
9 cargo build [options]
10
12 Compile local packages and all of their dependencies.
13
15 Package Selection
16 By default, when no package selection options are given, the packages
17 selected depend on the selected manifest file (based on the current
18 working directory if --manifest-path is not given). If the manifest is
19 the root of a workspace then the workspaces default members are
20 selected, otherwise only the package defined by the manifest will be
21 selected.
22
23 The default members of a workspace can be set explicitly with the
24 workspace.default-members key in the root manifest. If this is not set,
25 a virtual workspace will include all workspace members (equivalent to
26 passing --workspace), and a non-virtual workspace will include only the
27 root crate itself.
28
29 -p spec..., --package spec...
30 Build only the specified packages. See cargo-pkgid(1) for the SPEC
31 format. This flag may be specified multiple times and supports
32 common Unix glob patterns like *, ? and []. However, to avoid your
33 shell accidentally expanding glob patterns before Cargo handles
34 them, you must use single quotes or double quotes around each
35 pattern.
36
37 --workspace
38 Build all members in the workspace.
39
40 --all
41 Deprecated alias for --workspace.
42
43 --exclude SPEC...
44 Exclude the specified packages. Must be used in conjunction with
45 the --workspace flag. This flag may be specified multiple times and
46 supports common Unix glob patterns like *, ? and []. However, to
47 avoid your shell accidentally expanding glob patterns before Cargo
48 handles them, you must use single quotes or double quotes around
49 each pattern.
50
51 Target Selection
52 When no target selection options are given, cargo build will build all
53 binary and library targets of the selected packages. Binaries are
54 skipped if they have required-features that are missing.
55
56 Passing target selection flags will build only the specified targets.
57
58 Note that --bin, --example, --test and --bench flags also support
59 common Unix glob patterns like *, ? and []. However, to avoid your
60 shell accidentally expanding glob patterns before Cargo handles them,
61 you must use single quotes or double quotes around each glob pattern.
62
63 --lib
64 Build the package's library.
65
66 --bin name...
67 Build the specified binary. This flag may be specified multiple
68 times and supports common Unix glob patterns.
69
70 --bins
71 Build all binary targets.
72
73 --example name...
74 Build the specified example. This flag may be specified multiple
75 times and supports common Unix glob patterns.
76
77 --examples
78 Build all example targets.
79
80 --test name...
81 Build the specified integration test. This flag may be specified
82 multiple times and supports common Unix glob patterns.
83
84 --tests
85 Build all targets in test mode that have the test = true manifest
86 flag set. By default this includes the library and binaries built
87 as unittests, and integration tests. Be aware that this will also
88 build any required dependencies, so the lib target may be built
89 twice (once as a unittest, and once as a dependency for binaries,
90 integration tests, etc.). Targets may be enabled or disabled by
91 setting the test flag in the manifest settings for the target.
92
93 --bench name...
94 Build the specified benchmark. This flag may be specified multiple
95 times and supports common Unix glob patterns.
96
97 --benches
98 Build all targets in benchmark mode that have the bench = true
99 manifest flag set. By default this includes the library and
100 binaries built as benchmarks, and bench targets. Be aware that this
101 will also build any required dependencies, so the lib target may be
102 built twice (once as a benchmark, and once as a dependency for
103 binaries, benchmarks, etc.). Targets may be enabled or disabled by
104 setting the bench flag in the manifest settings for the target.
105
106 --all-targets
107 Build all targets. This is equivalent to specifying --lib --bins
108 --tests --benches --examples.
109
110 Feature Selection
111 The feature flags allow you to control which features are enabled. When
112 no feature options are given, the default feature is activated for
113 every selected package.
114
115 See the features documentation
116 <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>
117 for more details.
118
119 --features features
120 Space or comma separated list of features to activate. Features of
121 workspace members may be enabled with package-name/feature-name
122 syntax. This flag may be specified multiple times, which enables
123 all specified features.
124
125 --all-features
126 Activate all available features of all selected packages.
127
128 --no-default-features
129 Do not activate the default feature of the selected packages.
130
131 Compilation Options
132 --target triple
133 Build for the given architecture. The default is the host
134 architecture. The general format of the triple is
135 <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
136 a list of supported targets.
137
138 This may also be specified with the build.target config value
139 <https://doc.rust-lang.org/cargo/reference/config.html>.
140
141 Note that specifying this flag makes Cargo run in a different mode
142 where the target artifacts are placed in a separate directory. See
143 the build cache
144 <https://doc.rust-lang.org/cargo/guide/build-cache.html>
145 documentation for more details.
146
147 --release
148 Build optimized artifacts with the release profile. See the
149 PROFILES section for details on how this affects profile selection.
150
151 --ignore-rust-version
152 Build the target even if the selected Rust compiler is older than
153 the required Rust version as configured in the project's
154 rust-version field.
155
156 Output Options
157 --target-dir directory
158 Directory for all generated artifacts and intermediate files. May
159 also be specified with the CARGO_TARGET_DIR environment variable,
160 or the build.target-dir config value
161 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
162 to target in the root of the workspace.
163
164 --out-dir directory
165 Copy final artifacts to this directory.
166
167 This option is unstable and available only on the nightly channel
168 <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
169 requires the -Z unstable-options flag to enable. See
170 <https://github.com/rust-lang/cargo/issues/6790> for more
171 information.
172
173 Display Options
174 -v, --verbose
175 Use verbose output. May be specified twice for "very verbose"
176 output which includes extra output such as dependency warnings and
177 build script output. May also be specified with the term.verbose
178 config value
179 <https://doc.rust-lang.org/cargo/reference/config.html>.
180
181 -q, --quiet
182 No output printed to stdout.
183
184 --color when
185 Control when colored output is used. Valid values:
186
187 • auto (default): Automatically detect if color support is
188 available on the terminal.
189
190 • always: Always display colors.
191
192 • never: Never display colors.
193
194 May also be specified with the term.color config value
195 <https://doc.rust-lang.org/cargo/reference/config.html>.
196
197 --message-format fmt
198 The output format for diagnostic messages. Can be specified
199 multiple times and consists of comma-separated values. Valid
200 values:
201
202 • human (default): Display in a human-readable text format.
203 Conflicts with short and json.
204
205 • short: Emit shorter, human-readable text messages. Conflicts
206 with human and json.
207
208 • json: Emit JSON messages to stdout. See the reference
209 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
210 for more details. Conflicts with human and short.
211
212 • json-diagnostic-short: Ensure the rendered field of JSON
213 messages contains the "short" rendering from rustc. Cannot be
214 used with human or short.
215
216 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
217 messages contains embedded ANSI color codes for respecting
218 rustc's default color scheme. Cannot be used with human or
219 short.
220
221 • json-render-diagnostics: Instruct Cargo to not include rustc
222 diagnostics in in JSON messages printed, but instead Cargo
223 itself should render the JSON diagnostics coming from rustc.
224 Cargo's own JSON diagnostics and others coming from rustc are
225 still emitted. Cannot be used with human or short.
226
227 --build-plan
228 Outputs a series of JSON messages to stdout that indicate the
229 commands to run the build.
230
231 This option is unstable and available only on the nightly channel
232 <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
233 requires the -Z unstable-options flag to enable. See
234 <https://github.com/rust-lang/cargo/issues/5579> for more
235 information.
236
237 Manifest Options
238 --manifest-path path
239 Path to the Cargo.toml file. By default, Cargo searches for the
240 Cargo.toml file in the current directory or any parent directory.
241
242 --frozen, --locked
243 Either of these flags requires that the Cargo.lock file is
244 up-to-date. If the lock file is missing, or it needs to be updated,
245 Cargo will exit with an error. The --frozen flag also prevents
246 Cargo from attempting to access the network to determine if it is
247 out-of-date.
248
249 These may be used in environments where you want to assert that the
250 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
251 network access.
252
253 --offline
254 Prevents Cargo from accessing the network for any reason. Without
255 this flag, Cargo will stop with an error if it needs to access the
256 network and the network is not available. With this flag, Cargo
257 will attempt to proceed without the network if possible.
258
259 Beware that this may result in different dependency resolution than
260 online mode. Cargo will restrict itself to crates that are
261 downloaded locally, even if there might be a newer version as
262 indicated in the local copy of the index. See the cargo-fetch(1)
263 command to download dependencies before going offline.
264
265 May also be specified with the net.offline config value
266 <https://doc.rust-lang.org/cargo/reference/config.html>.
267
268 Common Options
269 +toolchain
270 If Cargo has been installed with rustup, and the first argument to
271 cargo begins with +, it will be interpreted as a rustup toolchain
272 name (such as +stable or +nightly). See the rustup documentation
273 <https://rust-lang.github.io/rustup/overrides.html> for more
274 information about how toolchain overrides work.
275
276 -h, --help
277 Prints help information.
278
279 -Z flag
280 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
281 details.
282
283 Miscellaneous Options
284 -j N, --jobs N
285 Number of parallel jobs to run. May also be specified with the
286 build.jobs config value
287 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
288 to the number of CPUs.
289
291 Profiles may be used to configure compiler options such as optimization
292 levels and debug settings. See the reference
293 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
294 details.
295
296 Profile selection depends on the target and crate being built. By
297 default the dev or test profiles are used. If the --release flag is
298 given, then the release or bench profiles are used.
299
300
301 ┌────────────────────┬─────────────────┬───────────────────┐
302 │Target │ Default Profile │ --release Profile │
303 ├────────────────────┼─────────────────┼───────────────────┤
304 │lib, bin, example │ dev │ release │
305 ├────────────────────┼─────────────────┼───────────────────┤
306 │test, bench, or any │ test │ bench │
307 │target in "test" or │ │ │
308 │"bench" mode │ │ │
309 └────────────────────┴─────────────────┴───────────────────┘
310
311 Dependencies use the dev/release profiles.
312
314 See the reference
315 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
316 for details on environment variables that Cargo reads.
317
319 • 0: Cargo succeeded.
320
321 • 101: Cargo failed to complete.
322
324 1. Build the local package and all of its dependencies:
325
326 cargo build
327
328 2. Build with optimizations:
329
330 cargo build --release
331
333 cargo(1), cargo-rustc(1)
334
335
336
337 CARGO-BUILD(1)