【发布时间】: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? 的可能重复项。