Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo test failed on no_std Rust #100766

Closed
XiangYyang opened this issue Aug 19, 2022 · 5 comments
Closed

cargo test failed on no_std Rust #100766

XiangYyang opened this issue Aug 19, 2022 · 5 comments

Comments

@XiangYyang
Copy link

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

@compiler-errors
Copy link
Member

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

@XiangYyang
Copy link
Author

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.

@compiler-errors
Copy link
Member

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

@XiangYyang
Copy link
Author

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.

@compiler-errors
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants