AArch64 的 Rust 裸机交叉编译:找不到 `core` 的箱子

Rust Bare-Metal Cross-Compilation for AArch64: can't find crate for `core`

我正在尝试交叉编译示例 rust 代码作为 Linux (KDE-Neon) 上裸机 AArch64 的库。不幸的是它不起作用。这是我的示例 Rust 代码 (lib.rs):

#![no_std]

#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
    a / 2
}

根据 [1],我首先安装了 rustup:

sudo snap install rustup --classic

后来,我关注了[2]和运行:

rustup toolchain list
rustup install stable
rustup default stable

然后我关注了[1]和[3]以及运行:

rustup target add aarch64-unknown-none

然而,当我之后尝试编译时,我无法工作,无论是 rustc 还是 cargo:

rustc:

rustc --crate-type=lib lib.rs --target=aarch64-unknown-none
error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-unknown-none` target may not be installed

error: aborting due to previous error

货物:

Cargo.toml:

[package]
name = "rust_baremetal_lib"
version = "0.1.0"
edition = "2018"

[lib]
name = "rust_baremetal_lib"
path = "src/lib.rs"
crate-type = ["staticlib"]

[dependencies]
cargo build --lib --target=aarch64-unknown-none
   Compiling rust_baremetal_lib v0.1.0 (/home/kilian/code/rust_link/rust_baremetal_lib)
error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-unknown-none` 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 `rust_baremetal_lib`

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

在我看来 rustc 和 cargo 找不到核心库,尽管它应该被安装,如 运行 rustc --print:

rustc --print target-list|grep arch64-unknown-none
aarch64-unknown-none
aarch64-unknown-none-softfloat

我已经在网上查过了,但遗憾的是没有找到任何线索。我希望有人能帮我找到问题!

[1] https://rust-lang.github.io/rustup/cross-compilation.html

[2]

[3]https://doc.rust-lang.org/nightly/rustc/platform-support.html

问题似乎是生锈安装损坏造成的。我删除了所有可以通过 apt 和 snap 找到的与 rust 相关的包。之后我通过推荐的方式重新安装了 rust [1]。然后我又 运行:

rustup target add aarch64-unknown-none

后来 Cargo 抱怨说我的示例代码中缺少“紧急处理程序”,所以我在 [2] 之后插入了一个。我的示例代码现在如下所示:

#![no_std]

use core::{
    panic::PanicInfo,
};

#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
    a * 2
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {
        continue;
    }
}

现在我终于可以将此示例交叉编译为 AArch64 裸机静态库:

cargo build --lib --target=aarch64-unknown-none

[1] https://www.rust-lang.org/tools/install

[2]https://interrupt.memfault.com/blog/zero-to-main-rust-1