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 Output Options
152 --target-dir directory
153 Directory for all generated artifacts and intermediate files. May
154 also be specified with the CARGO_TARGET_DIR environment variable,
155 or the build.target-dir config value
156 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
157 to target in the root of the workspace.
158
159 --out-dir directory
160 Copy final artifacts to this directory.
161
162 This option is unstable and available only on the nightly channel
163 <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
164 requires the -Z unstable-options flag to enable. See
165 https://github.com/rust-lang/cargo/issues/6790 for more
166 information.
167
168 Display Options
169 -v, --verbose
170 Use verbose output. May be specified twice for "very verbose"
171 output which includes extra output such as dependency warnings and
172 build script output. May also be specified with the term.verbose
173 config value
174 <https://doc.rust-lang.org/cargo/reference/config.html>.
175
176 -q, --quiet
177 No output printed to stdout.
178
179 --color when
180 Control when colored output is used. Valid values:
181
182 • auto (default): Automatically detect if color support is
183 available on the terminal.
184
185 • always: Always display colors.
186
187 • never: Never display colors.
188
189 May also be specified with the term.color config value
190 <https://doc.rust-lang.org/cargo/reference/config.html>.
191
192 --message-format fmt
193 The output format for diagnostic messages. Can be specified
194 multiple times and consists of comma-separated values. Valid
195 values:
196
197 • human (default): Display in a human-readable text format.
198 Conflicts with short and json.
199
200 • short: Emit shorter, human-readable text messages. Conflicts
201 with human and json.
202
203 • json: Emit JSON messages to stdout. See the reference
204 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
205 for more details. Conflicts with human and short.
206
207 • json-diagnostic-short: Ensure the rendered field of JSON
208 messages contains the "short" rendering from rustc. Cannot be
209 used with human or short.
210
211 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
212 messages contains embedded ANSI color codes for respecting
213 rustc's default color scheme. Cannot be used with human or
214 short.
215
216 • json-render-diagnostics: Instruct Cargo to not include rustc
217 diagnostics in in JSON messages printed, but instead Cargo
218 itself should render the JSON diagnostics coming from rustc.
219 Cargo's own JSON diagnostics and others coming from rustc are
220 still emitted. Cannot be used with human or short.
221
222 --build-plan
223 Outputs a series of JSON messages to stdout that indicate the
224 commands to run the build.
225
226 This option is unstable and available only on the nightly channel
227 <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
228 requires the -Z unstable-options flag to enable. See
229 <https://github.com/rust-lang/cargo/issues/5579> for more
230 information.
231
232 Manifest Options
233 --manifest-path path
234 Path to the Cargo.toml file. By default, Cargo searches for the
235 Cargo.toml file in the current directory or any parent directory.
236
237 --frozen, --locked
238 Either of these flags requires that the Cargo.lock file is
239 up-to-date. If the lock file is missing, or it needs to be updated,
240 Cargo will exit with an error. The --frozen flag also prevents
241 Cargo from attempting to access the network to determine if it is
242 out-of-date.
243
244 These may be used in environments where you want to assert that the
245 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
246 network access.
247
248 --offline
249 Prevents Cargo from accessing the network for any reason. Without
250 this flag, Cargo will stop with an error if it needs to access the
251 network and the network is not available. With this flag, Cargo
252 will attempt to proceed without the network if possible.
253
254 Beware that this may result in different dependency resolution than
255 online mode. Cargo will restrict itself to crates that are
256 downloaded locally, even if there might be a newer version as
257 indicated in the local copy of the index. See the cargo-fetch(1)
258 command to download dependencies before going offline.
259
260 May also be specified with the net.offline config value
261 <https://doc.rust-lang.org/cargo/reference/config.html>.
262
263 Common Options
264 +toolchain
265 If Cargo has been installed with rustup, and the first argument to
266 cargo begins with +, it will be interpreted as a rustup toolchain
267 name (such as +stable or +nightly). See the rustup documentation
268 <https://rust-lang.github.io/rustup/overrides.html> for more
269 information about how toolchain overrides work.
270
271 -h, --help
272 Prints help information.
273
274 -Z flag
275 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
276 details.
277
278 Miscellaneous Options
279 -j N, --jobs N
280 Number of parallel jobs to run. May also be specified with the
281 build.jobs config value
282 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
283 to the number of CPUs.
284
286 Profiles may be used to configure compiler options such as optimization
287 levels and debug settings. See the reference
288 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
289 details.
290
291 Profile selection depends on the target and crate being built. By
292 default the dev or test profiles are used. If the --release flag is
293 given, then the release or bench profiles are used.
294
295
296 ┌────────────────────┬─────────────────┬───────────────────┐
297 │Target │ Default Profile │ --release Profile │
298 ├────────────────────┼─────────────────┼───────────────────┤
299 │lib, bin, example │ dev │ release │
300 ├────────────────────┼─────────────────┼───────────────────┤
301 │test, bench, or any │ test │ bench │
302 │target in "test" or │ │ │
303 │"bench" mode │ │ │
304 └────────────────────┴─────────────────┴───────────────────┘
305
306 Dependencies use the dev/release profiles.
307
309 See the reference
310 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
311 for details on environment variables that Cargo reads.
312
314 • 0: Cargo succeeded.
315
316 • 101: Cargo failed to complete.
317
319 1. Build the local package and all of its dependencies:
320
321 cargo build
322
323 2. Build with optimizations:
324
325 cargo build --release
326
328 cargo(1), cargo-rustc(1)
329
330
331
332 CARGO-BUILD(1)