rust-lang/rust

cargo test failed on no_std Rust

Closed this issue · 5 comments

I'm using no_std Rust like this:

// lib.rs
#![cfg_attr(not(test), no_std)]

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

// others for no_std, such as panic_abort ... 

Then I found that the cargo test is failed:

$ cargo test
 ...
error: language item required, but not found: `eh_personality`
  |
  = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library
  = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config`

Here is my environment:

$ rustc --version
rustc 1.63.0 (4b91a6ea7 2022-08-08)

$ cargo --version
cargo 1.63.0 (fd9c4297c 2022-07-01)

A similar issue I found is #48665

What exactly is the bug here? The note seems pretty clear what the issue is.

What exactly is the bug here? The note seems pretty clear what the issue is.

Thanks for your reply.

In fact, I want to use the std crate in the test environment:

#![cfg_attr(not(test), no_std)]

What puzzled me was that it didn't seem to work.

What platform are you building this on? Does a cargo build succeed?

What platform are you building this on? Does a cargo build succeed?

Yes. cargo build --target x86_64-pc-windows-msvc is succeed.

But cargo test or cargo test --target x86_64-pc-windows-msvc is failed.

I think the key of this issue is cargo test was not linked std crate, and it's failed. So I tried #![cfg_attr(not(test), no_std)] to linking it and it does not work.

Sorry, I think I understand the issue. Yeah, it does look like #48665 is the same issue as this.