You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust嵌入式双核开发中依赖包导入未解决问题求助

STM32H755ZI Rust双核开发环境依赖未解析导入问题

我正在搭建STM32H755ZI的Rust嵌入式双核编程环境,此前单核MCU开发环境已搭建成功,但当前所有依赖包均显示未解析导入,尽管已在对应Cargo.toml中配置依赖。

编译报错信息

error[E0432]: unresolved import `panic_halt`
 --> cortex-m7/src/main.rs:5:5
  |
5 | use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch p...
  |     ^^^^^^^^^^^^^^^ no `panic_halt` in the root

error[E0432]: unresolved import `cortex_m`
  --> cortex-m7/src/main.rs:10:5
   |
10 | use cortex_m::asm;
   |     ^^^^^^^^ maybe a missing crate `cortex_m`?
   |
   = help: consider adding `extern crate cortex_m` to use the `cortex_m` crate

error[E0432]: unresolved import `cortex_m_rt`
  --> cortex-m7/src/main.rs:11:5
   |
11 | use cortex_m_rt::entry;
   |     ^^^^^^^^^^^ maybe a missing crate `cortex_m_rt`?
   |
   = help: consider adding `extern crate cortex_m_rt` to use the `cortex_m_rt` crate

error[E0432]: unresolved import `greeter`
  --> cortex-m7/src/main.rs:12:5
   |
12 | use greeter;
   |     ^^^^^^^ no `greeter` in the root

error: cannot determine resolution for the attribute macro `entry`
  --> cortex-m7/src/main.rs:14:3
   |
14 | #[entry]
   |   ^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: `#[panic_handler]` function required, but not found

For more information about this error, try `rustc --explain E0432`.
error: could not compile `cortex-m7` (bin "cortex-m7") due to 6 previous errors

环境配置详情

项目结构

.
|-- Cargo.lock
|-- Cargo.toml
|-- cortex-m4
|   |-- Cargo.toml
|   |-- build.rs
|   |-- memory.x
|   |-- openocd.cfg
|   |-- openocd.dbg
|   `-- src
|       `-- main.rs
|-- cortex-m7
|   |-- Cargo.toml
|   |-- build.rs
|   |-- memory.x
|   |-- openocd.cfg
|   |-- openocd.dbg
|   `-- src
|       `-- main.rs
`-- shared
    `-- greeter
        |-- Cargo.toml
        `-- src
            `-- lib.rs

main.rs代码(cortex-m7与cortex-m4一致)

#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
// use panic_abort as _; // requires nightly
// use panic_itm as _; // logs messages over ITM; requires ITM support
// use panic_semihosting as _; // logs messages to the host stderr; requires a debugger

use cortex_m::asm;
use cortex_m_rt::entry;

#[entry]
fn main() -> ! {
    asm::nop(); // To not have main optimize to abort in release mode, remove when you add code

    loop {
        // your code goes here
    }
}

cortex-m7/Cargo.toml

//cortex-m7/Cargo.toml
[package]
name = "cortex-m7"
version.workspace = true
authors.workspace = true
description.workspace = true

# Dependencis for bare metal dev
[dependencies]
cortex-m.workspace = true 
cortex-m-rt.workspace = true 
panic-halt.workspace = true 
#cortex-m-semihosting.workspace = true #un-comment to enable semihosting

### cortex-m7 bin config
[[bin]]
name = "cortex-m7"
path = "src/main.rs"

根目录Cargo.toml

### Workspaces
[workspace]
members = [ 
    "cortex-m7",
    "cortex-m4",
    "shared/greeter", 
]
default-members = ["cortex-m7"]
resolver = "2"

[workspace.package]
version = "0.1.0"
authors = ["Juan David Tangarife Hernandez <jdavidth.01@gmail.com>"]
description = "Dual core project for STM32H755ZI"

[workspace.dependencies]
cortex-m = "0.6.0"
cortex-m-rt = "0.6.10"
panic-halt = "0.2.0"
cortex-m-semihosting = "0.3.3" # un-comment to enable semihosting

### Debug build config
[profile.dev]
opt-level = 0  # No optimizations, full debug info

### Release build config
[profile.release]
codegen-units = 1 # better optimizations
debug = true # symbols are nice and they don't increase the size on Flash
lto = true # better optimizations

进入cortex-m7或cortex-m4目录执行cargo build时均出现上述依赖未找到错误,配置思路与单核环境一致但未解决问题,需排查原因。


解决方案
  • 从项目根目录执行编译命令:不要进入子crate目录单独编译,直接在根目录运行cargo build --package cortex-m7或cargo build --package cortex-m4。Cargo workspace依赖需要从根目录解析,子目录单独编译时无法正确识别workspace中的依赖定义。
  • 检查Cargo版本兼容性:workspace dependencies特性需要Cargo 1.60+版本支持,运行cargo --version确认版本,低于要求则升级Cargo。
  • 修复greeter导入问题:若要使用shared/greeter库,需在子crate的Cargo.toml中添加依赖:
    [dependencies]
    # 保留原有依赖
    greeter = { path = "../shared/greeter" }
    
    同时确保shared/greeter/Cargo.toml中正确设置[lib]条目(默认src/lib.rs会自动识别,显式声明更稳妥):
    [lib]
    name = "greeter"
    path = "src/lib.rs"
    
  • 清理缓存重新构建:运行cargo clean清除所有构建缓存,然后从根目录重新编译,避免缓存导致的依赖解析异常。

内容的提问来源于stack exchange,提问作者Juan_David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 06:37:37