编译错误:找不到`core`的箱子。

16
我使用 Rust 1.35.0 尝试 Rust 示例时编译失败,出现以下错误信息:
error[E0463]: can't find crate for `core`

我运行了rustc --explain E0463,并看到以下信息:

You need to link your code to the relevant crate in order to be able to use it
(through Cargo or the `-L` option of rustc example). Plugins are crates as
well, and you link to them the same way.

这是我的Cargo.toml文件:

[package]
name = "sensor-node"
version = "0.1.0"
authors = ["joesan <email@gmail.com>"]
edition = "2018"

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"

这是我的main.rs文件:

fn main() {
    let s = String::from("hello");  // s comes into scope

    takes_ownership(s);             // s's value moves into the function...
                                    // ... and so is no longer valid here

    let x = 5;                      // x comes into scope

    makes_copy(x);                  // x would move into the function,
                                    // but i32 is Copy, so it’s okay to still
                                    // use x afterward

} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) { // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) { // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.

如果你正在进行交叉编译 - https://os.phil-opp.com/cross-compile-libcore/ - doublesharp
如果您有正在跟随的示例链接,那也可能会有所帮助。 - doublesharp
这只是一个简单的程序,用于测试所有权概念!我已经在上面的帖子中添加了main.rs!没有什么特别的。 - joesan
rustup target add x86_64-unknown-linux-gnu 也没有帮助。 - joesan
我遇到了相同的错误,因为我的 shell 默认使用系统范围内的 cargo 安装在 /usr/local/bin/cargo,而不是 rustup 提供的安装。 - Mikko Ohtamaa
显示剩余2条评论
4个回答

4

你的代码在 Rust playground 上正常运行,因此我建议检查你的 Rust 安装和环境设置。


您可能想要使用预配置的 Rust Docker image 来运行您的应用程序。需要安装 Docker,然后执行以下步骤:

docker pull rust

转到您的项目文件夹并运行:

docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust cargo run

输出:

hello
5

对于在个人电脑上的简单示例,您不需要任何这些依赖项:

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"

以下是在Linux上测试您的样例的步骤:

cargo new hello
cd hello
code .

打开 main.rs,将示例的 main.rs 粘贴进去并保存:

fn main() {
    let s = String::from("hello"); // s comes into scope

    takes_ownership(s); // s's value moves into the function...
                        // ... and so is no longer valid here

    let x = 5; // x comes into scope

    makes_copy(x); // x would move into the function,
                   // but i32 is Copy, so it’s okay to still
                   // use x afterward
} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) {
    // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) {
    // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.

hello 文件夹中的终端中运行:
cargo run

输出结果很好:

hello
5

这可能有帮助:
  1. Shell command

    rustup component list --installed
    

    Output:

    cargo-x86_64-unknown-linux-gnu
    clippy-x86_64-unknown-linux-gnu
    rls-x86_64-unknown-linux-gnu
    rust-analysis-x86_64-unknown-linux-gnu
    rust-docs-x86_64-unknown-linux-gnu
    rust-src
    rust-std-x86_64-unknown-linux-gnu
    rustc-x86_64-unknown-linux-gnu
    rustfmt-x86_64-unknown-linux-gnu
    
  2. Shell command:

    rustup show
    

    Output:

    Default host: x86_64-unknown-linux-gnu
    
    installed toolchains
    --------------------
    
    stable-x86_64-unknown-linux-gnu (default)
    nightly-x86_64-unknown-linux-gnu
    
    active toolchain
    ----------------
    
    stable-x86_64-unknown-linux-gnu (default)
    rustc 1.35.0 (3c235d560 2019-05-20)
    

我在Linux上仍然遇到这个问题。 - chovy
@chovy:请查看这个这个 - wasmup
我已经尝试过第二个链接了。我收到了这个错误:=注意:/usr/bin/ld:/tmp/rustcPJoEQl/list:6:版本脚本中的语法错误 collect2: error: ld returned 1 exit status - chovy
@chovy 请查看此链接。你的代码在Rust playground上是否能正常运行? - wasmup
我对 Rust 并不是很了解,只是想让 Dfinity 的 Canister 正常工作。 - chovy
显示剩余8条评论

1
我在我的情况下解决了这个问题:
rustup target add wasm32-unknown-unknown

0
当您首次安装工具链时,rustup仅为您的主机平台安装标准库 - 也就是您当前运行的架构和操作系统。要编译到其他平台,您必须安装其他目标平台。这可以通过rustup target add命令完成。例如,要添加Android目标:
请参见Rustup Book - 交叉编译。
$ rustup target add aarch64-unknown-linux-gnu
info: downloading component 'rust-std' for 'aarch64-unknown-linux-gnu'
info: installing component 'rust-std' for 'aarch64-unknown-linux-gnu'

如果已经安装了Rust,您可以运行以下命令来在目标机器上获取目标字符串:

rustc -vV

例如输出

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: aarch64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

host字段可用作rustup target add的参数。

注意:您可能会遇到链接器问题,如果是这样,您可以配置您的链接器,请参见Cargo Book - Configuration - Target


0
当省略edition参数时,可能会出现此错误。在旧版本的Rust中,core crate不可用。虽然您在Cargo.toml文件中有该参数,但如果您尝试直接使用rustc命令行进行编译,该参数必须手动提供。例如:
rustc --edition 2018 <filename>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接