Add no_std target with rustup

Hello!
I am rather new to rust and this is my first post here.
I am currently trying to build a rust static library for a tier 3 target that only support >no_std< development.
When I run >rustc --print target-list< on my machine, the target I need is named in the list. When I try to add the target using >rustup target add (target_name)< I receive following Error:

component 'rust_std' for target 'target_name' is unavailable for download for channel 'stable'

At the same time, if I try to build using >Cargo build --release --target=target_name< I get an Error telling me that it >can't find crate for std< and >target might not be installed<

I could imagine just overlooking some basic fix in the documentation and the guides I have read so far, but I can't seem to get it to work. Thanks in advance for the help!


Edit: I could find some information about the !#[no_std] declaration, which I think helps my case. But even when adding !#[no_std] now the compiler says it can't find core, as the target still can't be found. So is there a way to add a target that doesn't support std, but supports core? (At least I hope my target supports core)

What is your target?

armv8r-none-eabihf, should've clarified that in the post

Looks like you have to build core from source for your target.

1 Like

Thank you! As far as I can see in the link you provided, adding these two lines in the cargo.toml file:

[unstable]
build-std = ["core"]

should be enough. My cargo.toml file looks like this at the moment:

[package]
name = "rust_lib"
version = "0.1.0"
edition = "2021"

[unstable]
build-std = ["core"]

[dependencies]

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

If I now run: cargo build --release --target = armv8r-none-eabihf
I get the same error as before: error[E0463] can't find crate for 'core'
This happens for the stable as well as nightly cargo

This does not belong in your Cargo.toml but in a .cargo/config.toml file.

1 Like

Okay, in my project folder I created a .cargo-Folder in which i created a config.toml file, where I inserted the two lines.
The outcome is still the same as before.

Are you using the nighly toolchain?

1 Like

I thought I was but it seems like I actually wasn't. I think it worked now, I followed the following steps:

(First follow the steps from the previous comments, regarding the config.toml file)

  1. Change into nightly cargo using "rustup override set nightly"
  2. run "rustup component add rust-src --toolchain nightly-x86_64-pc-windows-msvc" as my cargo build call told me to do this
  3. Insert a panic_handler funtion in my lib.rs source-file, I used this webpage to fix this: Rust no_std static lib panic_handler - Stack Overflow
  4. Then I could build. The new output that I need seems to be in target/armv8r-none-eabihf/release/librust_lib.a

Thank you again for the help!

This means you are building for your host platform. Try running cargo build --target=armv8r-none-eabihf to compile for your desired platform. Might be that you have to tell Cargo what linker it needs to use before it works.

To build my final library I use "cargo build --release --target=armv8r-none-eabihf"(line a)

The "rustup component add rust-src --toolchain nightly-x86_64-pc-windows-msvc"(line b) was used after I first changed to nightly cargo and tried to run line a. Cargo told me that some files are missing and I should run line b to get them. I did and it worked. Now when I build my library with line a it finishes and I receive a *.a-File, which should be what I am looking for

1 Like

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.