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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Aman Verma
  • 113
  • 1
  • 7
  • 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 09 '21 at 06:45
  • 2
    `rustup target add wasm32-unknown-unknown`? – Jmb Jun 09 '21 at 06:45

1 Answers1

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`
Cryptjar
  • 1,079
  • 8
  • 13
  • `error[E0433]: failed to resolve: use of undeclared type `Box`` and hundreds of others. – chovy Apr 29 '22 at 11:56
  • @chovy types like `Box` are part of the `alloc` crate not `core`, if you need `Box`es 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 01 '22 at 13:48