9

I get the following error message:

error[E0463]: can't find crate for `core`
  |
  = note: the `wasm32-unknown-unknown` target may not be installed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `cfg-if`

To learn more, run the command again with --verbose.

When I run this command:

cargo build --target wasm32-unknown-unknown
2
  • 5
    Are you sure that wasm32-unknown-unknown target is installed? If not, use rustup target add wasm32-unknown-unknown to install it.
    – Ivan C
    Jun 9, 2021 at 6:45
  • 2
    rustup target add wasm32-unknown-unknown?
    – Jmb
    Jun 9, 2021 at 6:45

1 Answer 1

9

There is a specialty about crates like core and std that is, since they are ubiquitous, they are used in some pre-compiled version instead of being build from source each time like other crates are. However, this only becomes apparent if you are cross compiling (i.e. using the --target flag).

There are essentially two options to solve this, and they are also explicitly stated in rustc --explain E0463 (online version):

  • [if] You are cross-compiling for a target which doesn’t have std [or core] prepackaged. Consider one of the following:
    • Adding a pre-compiled version of std [or core] with rustup target add
    • Building std [or core] from source with cargo build -Z build-std

The first option (also already pointed-out by @Ivan and @Jmb) is probably the obvious one:

rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

The second option only works with a Nightly compiler and requires the rust-src component. But might be useful if you don't want to install each target, or if you are compiling against a custom target (i.e. --target with some JSON-file):

rustup +nightly component add rust-src
cargo +nightly build --target wasm32-unknown-unknown -Z build-std=core

As a side note, the error message has improved in the current Nightly Rust version as opposed to the current stable version (1.52). A Nightly compiler will print:

error[E0463]: can't find crate for `core`
  |
  = note: the `wasm32-unknown-unknown` target may not be installed
  = help: consider downloading the target with `rustup target add wasm32-unknown-unknown`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`
2
  • error[E0433]: failed to resolve: use of undeclared type Box`` and hundreds of others.
    – chovy
    Apr 29, 2022 at 11:56
  • @chovy types like Box are part of the alloc crate not core, if you need Boxes you either have to install the target std crate (e.g. via rustup) or you need to also compile alloc (e.g. via build-std) and provide an allocator yourself.
    – Cryptjar
    May 1, 2022 at 13:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.