How to compile cdylib with stable version without `eh_personality` error?

Cargo.toml

[package]
name = "hello-world"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]

src/lib.rs

#![no_std]

use core::panic::PanicInfo;

/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

fn main() {}

error: language item required, but not found: eh_personality

See here. You should add panic = "abort" under [profile.dev] and [profile.release] in your Cargo.toml .

There are a couple of things in your code that come across as odd, so you might need to elaborate on where/how this code is meant to be used.

  1. The cdylib crate type is used when you want to create a shared library that exposes a C API that can be used from other languages, yet you've also defined a main function which is normally used as the entrypoint for an executable
  2. The program defines a main function, yet is #![no_std]. This means you want your code to run without using the operating system (typically on WebAssembly or bare metal devices without an OS), but when you are running without an OS the main function won't be used as your program's entry point (see the blog post linked earlier for more)
2 Likes

Thanks, but I've added it to my Cargo.toml,but it doesn't work

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

I am writing the Windows kernel driver,
the main function can be changed later when linking

In what way doesn't it work? You should use profile.debug not profile.dev, the dev profile is not a standard Cargo thing but is specific to that blog. [edit: this is wrong, I was mistaken about how Cargo works! you should indeed use profile.dev and profile.release]

but I've added it to my Cargo.toml,but it doesn't work

error: language item required, but not found: eh_personality

That's strange, with the Cargo.toml and lib.rs in your post I can compile successfully (you should never have fn main in lib.rs btw). Try running cargo clean and then building again? Or if that doesn't work, what version of rustc are you on?

$ rustc --version
rustc 1.55.0 (c8dfcfe04 2021-09-06)

I can't compile on both windows 10 and mac 11.6

Here is a repo where I've copied the Cargo.toml and lib.rs in your post. If I clone this and then cargo +1.55.0 build (disclaimer, I'm on x86_64-unknown-linux-gnu), I get no errors. What happens if you do that? If you can build that repo successfully, could you try running diff -q -x '*target*' eh-personality-error/ your-project/ and see if any differences are reported?

Thank you very much, I already know what the problem is.
Here is my repo.
It seems that the [workspace] in Cargo.toml should also be added

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
1 Like

Ah, you were inside a cargo workapace. [profile] for workspace members is ignored. Only the [profile] in the workspace root is used.

2 Likes

Indeed, and Cargo prints a warning about this.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.