【问题标题】:How to fix `definition of unknown language item 'panic_fmt'`? [duplicate]如何修复`未知语言项'panic_fmt'的定义`? [复制]
【发布时间】:2018-08-21 02:47:45
【问题描述】:

对于no_std 应用程序,在lang_items.rs 中定义了一些语言项,其中之一是panic_fmt 语言项(用于在此no_std 上下文中指定panic! 的行为)定义如下:

#[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }

编译时,我收到此错误:

error[E0522]: definition of an unknown language item: `panic_fmt`
 --> src/lang_items.rs:3:1
  |
3 | #[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }
  | ^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `panic_fmt`

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

阅读RFC 2070 后,我了解到no_std/嵌入式程序最近发生了重大变化。虽然建议我使用最近添加的功能 #[panic_implementation] 属性,但我仍然收到错误消息,如下所示:

#[panic_implementation] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }

给出错误:

error[E0658]: #[panic_implementation] is an unstable feature (see issue #44489)
 --> src/lang_items.rs:4:1
  |
4 | #[panic_implementation] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: add #![feature(panic_implementation)] to the crate attributes to enable

按照他们将#![feature(panic_implementation)] 添加到lang_items.rs 文件顶部的建议似乎并没有解决问题,因为我遇到了同样的错误。如何正确启用这个不稳定的功能,以便我可以编译这个 no_std 应用程序并定义 panic! 的行为?

【问题讨论】:

  • 什么是“lang_items.rs”?这不是标准的。
  • 请查看如何创建minimal reproducible example,然后查看edit 您的问题以包含它。我们无法分辨代码中存在哪些 crate、类型、特征、字段等。尝试在Rust Playground 上重现您的错误,或者您可以在全新的 Cargo 项目中重现它。还有Rust-specific MCVE tips
  • 你知道features 需要一个夜间编译器吗?
  • 是的,我正在使用夜间编译器 :-)。
  • 按照他们的建议,将#![feature(panic_implementation)] 添加到 lang_items.rs 文件的顶部——这不是建议。 What is a crate attribute and where do I add it? 的可能重复项。

标签: rust panic


【解决方案1】:

好吧,这是我犯的一个简单错误。这是包含我的代码的playground 的链接。首先,我需要将 crate 属性添加到我的 crate 根目录的 top(对我来说,它是 lib.rs)。 lang_items.rs 只是在lib.rs 中使用的一个文件,如下所示:

#![feature(compiler_builtins_lib, lang_items, asm, panic_implementation, core_intrinsics)]
#![no_builtins]
#![no_std]

pub mod lang_items;

const GPIO_BASE: usize = 0x3F000000 + 0x200000;

const GPIO_FSEL1: *mut u32 = (GPIO_BASE + 0x04) as *mut u32;
const GPIO_SET0: *mut u32 = (GPIO_BASE + 0x1C) as *mut u32;
const GPIO_CLR0: *mut u32 = (GPIO_BASE + 0x28) as *mut u32;

#[inline(never)]
fn spin_sleep_ms(ms: usize) {
    for _ in 0..(ms * 600) {
        unsafe { asm!("nop" :::: "volatile"); }
    }
}

#[no_mangle]
pub unsafe extern "C" fn kmain() {
    // STEP 1: Set GPIO Pin 16 as output.
    GPIO_FSEL1.write_volatile(0x1 << 0x12);
    // STEP 2: Continuously set and clear GPIO 16.
    loop {
        GPIO_SET0.write_volatile(0x1 << 0x10);
        spin_sleep_ms(256);
        GPIO_CLR0.write_volatile(0x1 << 0x10);
        spin_sleep_ms(256);
    }
}

【讨论】:

  • 当现有问答已经解决了问题时,无需重复答案。这只会增加保持问答有用所需的维护量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 2021-05-11
  • 2017-03-10
  • 2018-03-10
  • 2019-09-16
  • 2019-09-02
相关资源
最近更新 更多