Rust 自定义panic_fmt导致发现重复的 lang 项 'panic_fmt' lang 项首先在 crate 'core' 中定义

Rust custom panic_fmt lead to found duplicate lang item `panic_fmt` the lang item is first defined in crate `core`

提问人:Tristan 提问时间:10/29/2023 更新时间:10/29/2023 访问量:42

问:

在使用 Rust 的嵌入式项目中,当我尝试实现自己的恐慌处理程序时,我目前面临以下问题。

为了给你一些背景信息,我目前正在用 rust nightly 版本构建我的项目。 代码在这里:

#![no_std] //Disable standard lib
#![no_main]
#![feature(lang_items)]

#[lang = "panic_fmt"]
#[lang = "eh_personality"] extern fn eh_personality() {}
#[no_mangle]
pub extern fn panic_fmt(fmt: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
    println!("\n\nPANIC in {} at line {}:", file, line);
    println!("    {}", fmt);
    loop{}
}

这是我的部分 Cargo.toml :

[lib]
crate-type = ["staticlib"]

[dependencies]
multiboot2 = { version = "0.19", default-features = false }
rlibc = "1.0.0"
spin = "0.9.8"
volatile = "0.2.7"
x86_64 = "0.14.11"

[dependencies.lazy_static]
version = "1.4.0"
features = ["spin_no_std"]

和我的目标:

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

编译时,出现以下错误: panic_fmt导致找到重复的 lang 项,首先在 crate 中定义 lang 项panic_fmtcore

我不明白为什么

我试图从 Rust 官方存储库中查看一些问题,但没有什么与我的情况真正相似。

Rust 嵌入式 标准库 rust-crates nightly-build

评论

1赞 Cerberus 10/29/2023
为什么要实现语言项(而不仅仅是设置恐慌处理程序)?它应该只在核心中,不会被任何东西覆盖。panic_fmt
0赞 Tristan 10/30/2023
但真正的问题是为什么我不能?我想为这个处理程序实现我自己的行为
0赞 Colonel Thirty Two 10/31/2023
支持覆盖恐慌处理程序。不支持覆盖函数,该函数不是处理程序 - 它只是一个函数。panic_fmt
0赞 Cerberus 10/31/2023
好吧,可以覆盖(或者更准确地说,在自己的代码中声明),因为可以去 .但这唯一合理的情况可能是当你 .panic_fmt#[no_core]core

答: 暂无答案