STM32 NUCLEO-U575ZI-Q项目Rust编译报错:找不到`core` crate
解决STM32 NUCLEO-U575ZI-Q Rust Blinky项目编译错误(E0463)
问题概述
基于STM32 NUCLEO-U575ZI-Q开发入门级Rust Blinky项目,编译时触发E0463错误,提示找不到core crate。已执行rustup target add thumbv8m.main-none-eabihf但问题未解决,调试时提示cargo invocation has failed: Error: exit code: 101,可参考资料多为5年以上旧内容。
环境信息
- Ubuntu 22.04.4 LTS
- Linux 6.8.0-40-generic
- Docker容器内通过以下命令安装Rust:
RUN curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y
编译错误日志
Compiling nb v1.1.0 Compiling vcell v0.1.3 Compiling void v1.0.2 Compiling bare-metal v0.2.5 error[E0463]: can't find crate for `core` | = note: the `thumbv8m.main-none-eabihf` target may not be installed = help: consider downloading the target with `rustup target add thumbv8m.main-none-eabihf` error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0463`. error: could not compile `bare-metal` (lib) due to 2 previous errors warning: build failed, waiting for other jobs to finish... error[E0463]: can't find crate for `core` | = note: the `thumbv8m.main-none-eabihf` target may not be installed = help: consider downloading the target with `rustup target add thumbv8m.main-none-eabihf` error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0463`. error[E0463]: can't find crate for `core` | = note: the `thumbv8m.main-none-eabihf` target may not be installed = help: consider downloading the target with `rustup target add thumbv8m.main-none-eabihf` error: could not compile `nb` (lib) due to 2 previous errors error: aborting due to 1 previous error error[E0463]: can't find crate for `core` | = note: the `thumbv8m.main-none-eabihf` target may not be installed = help: consider downloading the target with `rustup target add thumbv8m.main-none-eabihf` For more information about this error, try `rustc --explain E0463`. error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0463`. error: could not compile `void` (lib) due to 2 previous errors error: could not compile `vcell` (lib) due to 2 previous errors
项目结构
. ├── blinky │ ├── build.rs │ ├── Cargo.lock │ ├── Cargo.toml │ ├── examples │ ├── memory.x │ ├── openocd.cfg │ ├── openocd.gdb │ ├── README.md │ ├── src │ └── target ├── devManager.sh ├── docker │ └── Dockerfile ├── stm32u575_pac │ ├── build.rs │ ├── Cargo.lock │ ├── Cargo.toml │ ├── device.x │ ├── src │ ├── stm32u575.svd │ └── target └── svd └── stm32u575.svd 9 directories, 15 files
已尝试操作
- 执行
rustup target add thumbv8m.main-none-eabihf
相关配置文件
launch.json
{ /* * Requires the Rust Language Server (rust-analyzer) and Cortex-Debug extensions * https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer * https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug */ "version": "0.2.0", "configurations": [ { "type": "cortex-debug", "request": "launch", "name": "Debug (QEMU)", "servertype": "qemu", "cwd": "${workspaceRoot}", "preLaunchTask": "Cargo Build (debug)", "runToEntryPoint": "main", "executable": "./target/thumbv7m-none-eabi/debug/blinky", /* Run `cargo build --example hello` and uncomment this line to run semi-hosting example */ //"executable": "./target/thumbv7m-none-eabi/debug/examples/hello", "cpu": "cortex-m3", "machine": "lm3s6965evb", }, { /* Configuration for the STM32F303 Discovery board */ "type": "cortex-debug", "request": "launch", "name": "Debug (OpenOCD)", "servertype": "openocd", "cwd": "${workspaceRoot}", "preLaunchTask": "Cargo Build (debug)", "runToEntryPoint": "main", "executable": "./target/thumbv8m.main-none-eabihf/debug/blinky", /* Run `cargo build --example itm` and uncomment this line to run itm example */ // "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm", "device": "STM32U575ZIT", "configFiles": [ "interface/stlink-v2-1.cfg", "target/stm32u5x.cfg" ], "svdFile": "${workspaceRoot}/.vscode/stmu575.svd", "swoConfig": { "enabled": true, "cpuFrequency": 8000000, "swoFrequency": 2000000, "source": "probe", "decoders": [ { "type": "console", "label": "ITM", "port": 0 } ] } } ] }
Dockerfile
# Use the latest version of Ubuntu as the base image FROM ubuntu:22.04 # Set the maintainer label LABEL maintainer="dvillalva21@gmail.com" # Set environment variables to non-interactive to prevent prompts ENV DEBIAN_FRONTEND=non-interactive # Update package lists, install basic tools, toolchains, stlink-tools, and clean up in one RUN command RUN apt-get update -y && \ apt-get install -y --no-install-recommends \ autoconf \ automake \ curl \ build-essential \ git \ libtool \ make \ pkg-config \ ca-certificates \ software-properties-common \ xz-utils \ libssl-dev && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Install Rust using rustup RUN curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y # Add Rust to the PATH environment variable ENV PATH=/root/.cargo/bin:$PATH # cargo-binutils is a collection of Cargo subcommands that make # it easy to use the LLVM tools that are shipped with the Rust toolchain. # These tools include the LLVM versions of objdump, nm and size and are # used for inspecting binaries. # referece: https://docs.rust-embedded.org/book/intro/tooling.html RUN cargo install cargo-binutils RUN rustup component add llvm-tools # cargo-generate is a developer tool to help you get up and running quickly # with a new Rust project by leveraging a pre-existing git repository as a template. RUN cargo install cargo-generate # svd2rust is a tool to generate Rust register maps (structs) from SVD files. # You can use this tool to generate a Peripheral Access Crate (PAC) for your microcontroller. RUN cargo install svd2rust # form is a tool to format Rust code according to a style guide. # It is a wrapper around rustfmt and can be used to format code in a consistent manner. RUN cargo install form #Cortex-M33F with FPU (ARMv8-M Mainline architecture): RUN rustup target add thumbv8m.main-none-eabihf # Setup a tool directory in /home/dev and download the ARM toolchain WORKDIR /home/dev # Install GNU Arm toolchain (Version 13.2.Rel1) RUN curl -LO https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz && \ tar xf arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz && \ rm arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz # Add the GNU Arm toolchain to the PATH ENV PATH="/home/dev/arm-gnu-toolchain-13.2.rel1/bin:${PATH}" # Create and set the working directory to /home/app WORKDIR /home/app # Set the default command to bash CMD ["/bin/bash"]
settings.json
{ "workbench.colorTheme": "Default High Contrast", "git.openRepositoryInParentFolders": "never", "cortex-debug.armToolchainPath": "/home/dev/arm-gnu-toolchain-13.2.rel1/bin" }
解决方案建议
- 确认Docker容器内目标安装状态
进入Docker容器,执行以下命令检查目标是否正确安装:
rustup target list | grep thumbv8m.main-none-eabihf
若未显示(installed),重新执行安装命令并检查日志是否有报错:
rustup target add thumbv8m.main-none-eabihf
- 配置Cargo默认目标与链接参数
在blinky项目根目录创建.cargo/config.toml文件,添加以下内容:
[build] target = "thumbv8m.main-none-eabihf" [target.thumbv8m.main-none-eabihf] rustflags = [ "-C", "link-arg=-Tmemory.x", ]
确保Cargo编译时自动使用指定目标,并加载memory.x链接脚本。
- 验证build.rs脚本正确性
检查blinky项目的build.rs是否正确处理memory.x文件,确保内容类似:
use std::env; use std::fs::File; use std::io::Write; use std::path::PathBuf; fn main() { let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); File::create(out.join("memory.x")) .unwrap() .write_all(include_bytes!("memory.x")) .unwrap(); println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rerun-if-changed=memory.x"); }
重新构建Docker镜像
删除旧镜像,重新构建Docker镜像,确保rustup target add thumbv8m.main-none-eabihf命令执行成功,无隐藏报错。显式指定目标编译
编译时显式指定目标,避免默认目标不匹配:
cargo build --target thumbv8m.main-none-eabihf
- 检查PAC crate依赖配置
确认blinky项目的Cargo.toml中正确依赖本地stm32u575_pac,例如:
[dependencies] stm32u575_pac = { path = "../stm32u575_pac" }
内容的提问来源于stack exchange,提问作者blurry
相关产品推荐
相关产品推荐

