在ST Nucleo-L152RE上使用rtt-target crate编译失败求助
解决ST Nucleo-L152RE Rust项目中rtt-target链接错误问题
问题描述
我在针对ST Nucleo-L152RE的Rust嵌入式项目中引入rtt-target crate,添加rtt_init_print!()和rprintln!()语句后,执行cargo build时编译失败,出现链接错误。相关文件及错误信息如下:
Cargo.toml
# Cargo.toml [package] edition = "2018" name = "blinky-rust" version = "0.1.0" [profile.release] opt-level = 'z' # turn on maximum optimizations. We only have 64kB lto = true # Link-time-optimizations for further size reduction [dependencies] cortex-m = "^0.6.3" # Access to the generic ARM peripherals cortex-m-rt = "^0.6.12" # Startup code for the ARM Core embedded-hal = "^0.2.4" # Access to generic embedded functions (`set_high`) panic-halt = "^0.2.0" # Panic handler rtt-target = "0.5.0" # Access to the HAL. [dependencies.stm32l1xx-hal] features = ["stm32l151", "rt"] version = "^0.0.1"
main.rs
# main.rs #![no_main] #![no_std] use panic_halt as _; use cortex_m_rt::entry; use stm32l1xx_hal::delay::Delay; use stm32l1xx_hal::gpio::GpioExt; use stm32l1xx_hal::hal::digital::v2::OutputPin; use stm32l1xx_hal::rcc::{Config, RccExt}; use stm32l1xx_hal::stm32::Peripherals; use stm32l1xx_hal::stm32::CorePeripherals; use stm32l1xx_hal::time::MicroSeconds; use rtt_target::{rtt_init_print, rprintln}; #[entry] fn main() -> ! { rtt_init_print!(); let p = Peripherals::take().unwrap(); let cp = CorePeripherals::take().unwrap(); // Get LED pin PB6 let gpioa = p.GPIOA.split(); let mut led = gpioa.pa5.into_push_pull_output(); // Set up a delay let rcc = p.RCC.freeze(Config::default()); let mut delay = Delay::new(cp.SYST, rcc.clocks); loop { rprintln!("Hello, world!"); // Turn LED On led.set_high().unwrap(); delay.delay(MicroSeconds(2_000_000_u32)); // Turn LED Off led.set_low().unwrap(); delay.delay(MicroSeconds(1_000_000_u32)); } }
Embed.toml
# Embed.toml [default.general] chip = "STM32L152RE" [default.rtt] enabled = true [default.probe] protocol = "Swd" [default.gdb] enabled = false
编译错误信息
Compiling blinky-rust v0.1.0 (/home/alex/projects/cortex-m-quickstart) error: linking with `arm-none-eabi-ld` failed: exit status: 1 = note: arm-none-eabi-ld: /home/alex/projects/cortex-m-quickstart/target/thumbv7m-none-eabi/debug/deps/librtt_target-df1e3f299aa0549b.rlib(rtt_target-df1e3f299aa0549b.rtt_target.1a0c5dea5ce969ca-cgu.0.rcgu.o): in function `critical_section::acquire': /home/alex/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:183: undefined reference to `_critical_section_1_0_acquire' arm-none-eabi-ld: /home/alex/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:183: undefined reference to `_critical_section_1_0_acquire' arm-none-eabi-ld: /home/alex/projects/cortex-m-quickstart/target/thumbv7m-none-eabi/debug/deps/librtt_target-df1e3f299aa0549b.rlib(rtt_target-df1e3f299aa0549b.rtt_target.1a0c5dea5ce969ca-cgu.0.rcgu.o): in function `critical_section::release': /home/alex/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200: undefined reference to `_critical_section_1_0_release' = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified = note: use the `-l` flag to specify native libraries to link = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname) error: could not compile `blinky-rust` (bin "blinky-rust") due to previous error
解决方案
错误原因
链接错误提示找不到_critical_section_1_0_acquire和_critical_section_1_0_release符号,这是因为rtt-target v0.5.0依赖critical-section crate,但当前项目没有提供该crate的具体实现——在旧版Cortex-M Rust生态中,critical-section的实现需要显式引入。
修复步骤
- 添加critical-section实现依赖
在Cargo.toml的[dependencies]区块中添加以下内容:
critical-section = "1.1.2" critical-section-cortex-m = "1.1.2"
- 启用critical-section实现
在main.rs的开头(use panic_halt as _;之后)添加一行代码,确保实现被链接到项目:
use critical_section_cortex_m as _;
- 可选:升级依赖版本(推荐)
当前项目使用的stm32l1xx-hal版本^0.0.1过于老旧,建议升级到稳定版本(如0.15.0),同时同步更新其他依赖以保证兼容性:
[dependencies] cortex-m = "0.7.7" cortex-m-rt = "0.7.3" embedded-hal = "0.2.7" panic-halt = "0.2.0" rtt-target = "0.5.0" critical-section = "1.1.2" critical-section-cortex-m = "1.1.2" [dependencies.stm32l1xx-hal] features = ["stm32l152re", "rt"] version = "0.15.0"
注意:升级HAL版本后,可能需要调整main.rs中的部分API调用(比如Delay初始化、GPIO拆分逻辑),具体参考对应版本的HAL文档。
- 重新编译
执行cargo build,此时链接错误应被修复,之后即可通过RTT工具查看设备日志。
内容的提问来源于stack exchange,提问作者Oleksandr
相关产品推荐
相关产品推荐

