0182-独立可执行程序

环境

  • Time 2022-11-12
  • WSL-Ubuntu 22.04
  • QEMU 6.2.0
  • Rust 1.65.0

前言

说明

参考:https://os.phil-opp.com/freestanding-rust-binary

目标

当前为新的篇章,参考的原文第二版,和之前的第一版不关联。
使用 Rust 编写一个独立可执行程序,即不依赖操作系统的程序。
其中的概念和错误的解决方式可以参考原文,有中文翻译。

main.rs

#![no_std]
#![no_main]

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

#[cfg(not(test))] // 避免 vscode 提示错误
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

Cargo.toml

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

[dependencies]


[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

编译

root@jiangbo12490:~/git/game# cargo build --target thumbv7em-none-eabihf
   Compiling mos v0.1.0 (/root/git/game)
    Finished dev [unoptimized + debuginfo] target(s) in 0.23s

总结

使用 Rust 编写了一个独立可执行程序。

附录

posted @ 2024-07-15 09:09  jiangbo4444  阅读(27)  评论(0)    收藏  举报