嵌入式Rust小探

说明

现在很多项目都在从 C++/C 迁移到 Rust, 这是一种内存管理更安全的语言, 从根本上可以防止一些指针操作的问题

既然如此, 那我们也尝试一下如何在嵌入式开发当中使用Rust, 然后再慢慢迁移到更多代码

https://doc.rust-lang.org/reference/introduction.html

https://zhuanlan.zhihu.com/p/628575325

https://www.rust-lang.org/what/embedded

https://docs.rust-embedded.org/book/ => https://xxchang.github.io/book/

安装

PC X86编译

  • 创建代码main.rs

    fn main() {
    println!("Hello, world!");
    }

  • 编译代码

    rustc main.rs

  • 执行代码

    ./main

    Hello, world!

    这个过程与gcc一致

嵌入式裸编译

  • 在设备上验证

    source build/envsetup.sh
    rustc --target arm-unknown-linux-gnueabihf main.rs -C linker=arm-ca53-linux-gnueabihf-gcc

    root@NVTEVM:/bin$ ./main
    Hello, world!

  • 在RV1106上面
    uclibc 不支持的原因, 暂时未知如何处理

Cargo脚本编译

https://doc.rust-lang.org/cargo/reference/config.html

创建工程

复制代码
cargo new cargoprj
cd cargoprj

配置文件

Cargo.toml

复制代码
[package]
name = "cargoprj"
version = "0.1.0"
edition = "2021"

[dependencies]

生成X86

复制代码
cargo build

target/debug/cargoprj

修改配置支持嵌入式编译

增加配置文件.cargo/config.toml指定target和linker

复制代码
$ cat .cargo/config.toml
[build]
target = "arm-unknown-linux-gnueabihf"

[target.arm-unknown-linux-gnueabihf]
linker = "arm-ca53-linux-gnueabihf-gcc"

$cargo build
生成
target/arm-unknown-linux-gnueabihf/debug/cargoprj

嵌入式cmake集成

TODO:

常用命令

当前安装的rust版本, 支持的target

rustup show

列出所有支持的target

rustup target list

安装需要的平台

rustup target add arm-unknown-linux-gnueabihf

问题

  • RV1106的uclibc不支持

    $ rustup target add armv7-unknown-linux-uclibceabi
    error: toolchain 'stable-x86_64-unknown-linux-gnu' does not support target 'armv7-unknown-linux-uclibceabi'
    note: you can see a list of supported targets with rustc --print=target-list
    note: if you are adding support for a new target to rustc itself, see https://rustc-dev-guide.rust-lang.org/building/new-target.html

相关推荐
阿正的梦工坊6 小时前
【Rust】12-借用检查器与非词法生命周期
开发语言·后端·rust
吴佳浩8 小时前
炸裂!!!给 codeX 装上本地大脑:cc-switch_Ollama 接入全记录
人工智能·rust·openai
阿正的梦工坊11 小时前
【Rust】19-FFI、ABI 与跨语言边界设计
开发语言·后端·rust
fox_lht11 小时前
第十五章 函数式语言:迭代器和闭包
开发语言·后端·学习·算法·rust
shencangsheng12 小时前
从 Polars 到 DataFusion:一个 Rust 桌面 SQL 查询工具的技术演进
rust
fox_lht13 小时前
14.6.将错误重定向到标准错误
开发语言·后端·学习·rust
wzg19690226wzg13 小时前
rust 学习 泛型
开发语言·学习·rust
techdashen13 小时前
Rust 基础设施团队 2025 Q4 回顾与 2026 Q1 计划
开发语言·后端·rust
阿正的梦工坊14 小时前
【Rust】17-Send、Sync 与并发安全抽象
算法·安全·rust
阿正的梦工坊15 小时前
【Rust】13-Trait 系统、动态分发与对象安全
算法·安全·rust