错误[E0463]:在为 wasm32-unknown-unknown 构建 Rust 项目时找不到“core”的包

Ama*_*rma 9 compilation rust rust-cargo webassembly wasm-bindgen

我收到以下错误消息:

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.
Run Code Online (Sandbox Code Playgroud)

当我运行这个命令时:

cargo build --target wasm32-unknown-unknown
Run Code Online (Sandbox Code Playgroud)

Cry*_*jar 9

core像和这样的板条箱有一个特殊之处std,那就是,由于它们无处不在,因此它们在某些预编译版本中使用,而不是像其他板条箱那样每次都从源代码构建。但是,只有在交叉编译(即使用标志--target)时,这一点才会变得明显。

\n

本质上有两个选项可以解决这个问题,并且在rustc --explain E0463在线版本)中也明确说明了它们:

\n
\n
    \n
  • [if] 您正在对未\xe2\x80\x99t std[或core] 预先打包的目标进行交叉编译。请考虑以下其中一项:\n
      \n
    • std添加[or core]的预编译版本rustup target add
    • \n
    • 使用货物构建从源代码构建std[或]core-Z build-std
    • \n
    \n
  • \n
\n
\n

第一个选项(@Ivan 和 @Jmb 也已经指出)可能是显而易见的\none:

\n
rustup target add wasm32-unknown-unknown\ncargo build --target wasm32-unknown-unknown\n
Run Code Online (Sandbox Code Playgroud)\n

第二个选项仅适用于 Nightly 编译器并且需要该rust-src组件。但如果您不想安装每个目标,或者您正在针对自定义目标(即--target使用某些 JSON 文件)进行编译,则可能会很有用:

\n
rustup +nightly component add rust-src\ncargo +nightly build --target wasm32-unknown-unknown -Z build-std=core\n
Run Code Online (Sandbox Code Playgroud)\n
\n

附带说明一下,与当前稳定版本 (1.52) 相比,当前 Nightly Rust 版本中的错误消息已得到改进。每晚编译器将打印:

\n
error[E0463]: can\'t find crate for `core`\n  |\n  = note: the `wasm32-unknown-unknown` target may not be installed\n  = help: consider downloading the target with `rustup target add wasm32-unknown-unknown`\n  = help: consider building the standard library from source with `cargo build -Zbuild-std`\n
Run Code Online (Sandbox Code Playgroud)\n