如何修复未知语言项目“ panic_fmt”的定义?

时间:2018-08-20 18:28:54

标签: rust panic

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

#[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!应用程序?

1 个答案:

答案 0 :(得分:0)

好的,这是我犯的一个简单错误。这是包含我的代码的playground的链接。首先,我需要将板条箱属性添加到板条箱根(对于我来说是lib.rs top )中。 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);
    }
}