This content originally appeared on DEV Community and was authored by ender minyard
There are many in-depth tutorials that explain how to write a #![no_std]
binary. This is just a simple, reusable bare-bones template that works for me.
In your src/main.rs
:
#![no_std]
#![no_main]
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {}
}
In your Cargo.toml
:
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
Instead of simply running cargo build
to generate a binary like usual, you need to jump through some hoops to generate a binary.
If you are compiling this binary for Linux, you can run:
cargo rustc -- -C link-arg=-nostartfiles
For Windows:
cargo rustc -- -C link-args="/ENTRY:_start /SUBSYSTEM:console"
For macOS:
cargo rustc -- -C link-args="-e __start -static -nostartfiles"
All of this is useful if you plan on running the resulting binary on bare metal. If you're compiling Rust to WebAssembly, none of this is necessary - simply not using the std
library is enough for that use case.
If you're targeting the WebAssembly System Interface, it's more simple and you can even use Rust's std
library for that use case.
This content originally appeared on DEV Community and was authored by ender minyard
ender minyard | Sciencx (2021-04-07T01:48:41+00:00) Rust no_std template. Retrieved from https://www.scien.cx/2021/04/07/rust-no_std-template/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.