交叉编译Rust为树莓派与sysroot



我试图交叉编译我的rust程序的树莓派v3与systemroot从https://github.com/raspberrypi/tools。但是我得到了以下错误:

error[E0463]: can't find crate for `core`
|
= note: the `armv7-unknown-linux-gnueabihf` target may not be installed
= help: consider downloading the target with `rustup target add armv7-unknown-linux-gnueabihf`
error[E0463]: can't find crate for `compiler_builtins`
error[E0463]: can't find crate for `core`
--> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/bitflags-1.3.2/src/lib.rs:282:1
|
282 | pub extern crate core as _core;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `armv7-unknown-linux-gnueabihf` target may not be installed
= help: consider downloading the target with `rustup target add armv7-unknown-linux-gnueabihf`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `bitflags` due to 3 previous errors

我检查了armv7-unknown-linux-gnueabihf已经正确安装。我没有在cargo配置中指定sysroot。错误将会消失。这是我的.cargo/config.toml

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
rustflags = ["--sysroot", "/code/pi-tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/arm-bcm2708hardfp-linux-gnueabi/sysroot"]

有人有使用sysroot交叉编译rust的经验吗?我使用sysroot的原因是我需要使用一些共享库来编译rust程序。或者在这些情况下编译rust的标准方法是什么?我是第一次在树莓派上使用rust。

——sysroot"不能在rustflags中直接设置,因为rustc编译器会使用这个参数,并认为Rust标准库也应该在这个sysroot中搜索。这就是为什么你得到箱子'core'不能找到的错误。

sysroot参数只有在与raspberry pi sysroot连接时才需要设置。所以这里应该使用link-arg。.cargo/config的工作示例。toml是:

[target.arm-unknown-linux-gnueabihf]
linker="arm-poky-linux-gnueabi-gcc"
rustflags = [
"-C", "link-arg=-march=armv6",
"-C", "link-arg=-mfpu=vfp",
"-C", "link-arg=-mfloat-abi=hard",
"-C", "link-arg=-mtune=arm1176jzf-s",
"-C", "link-arg=-mfpu=vfp",
"-C", "link-arg=--sysroot=your-sysroot-path",
]

另一种使用env而不是.cargo/config的方法。toml是:

/* Used by Rust cross-compilation */
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=${CC_PREFIX}gcc"
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-mcpu=cortex-a72.cortex-a53 -C link-arg=-march=armv8-a+crc -C link-arg=--sysroot=${TARGET_SYSROOT}"
export CARGO_BUILD_TARGET=aarch64-unknown-linux-gnu

然后"货舱";将对目标CARGO_BUILD_TARGET进行交叉编译。

注意:两个环境名称CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER/CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS应该更改为您自己的目标体系结构。因此,如果目标是armv7-unknown-linux-gnueabihf,则环境变量应该是CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER。

rustc文档中的一个交叉编译示例供参考。

最新更新