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