mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-06 17:12:01 +08:00
Writing a Rust Dynamic Library Usable by RT-Thread
- Set up the project
Create a new
cargoproject as usual. There are some flags to tellcargoto generate a system library instead of a regular Rust target.
[lib]
name = "your_crate"
crate-type = ["cdylib"] # generate a dynamic library
# crate-type = ["staticlib"] # generate a static library
Add the following to config.toml to specify the target platform:
[build]
target = "your_target"
- Enable RT-Thread dynamic module loading
To load dynamic modules in RT-Thread, enable it in
Kconfig:
RT-Thread Components → C/C++ and POSIX layer
→ POSIX (Portable Operating System Interface) layer
→ Enable dynamic module APIs, dlopen()/dlsym()/dlclose() etc
- Enable the filesystem
To place dynamic modules into RT-Thread, enable a filesystem in
Kconfig:
RT-Thread online packages → system packages
→ lwext4: an excellent choice of ext2/3/4 filesystem for microcontrollers.
Using the rust library
Add rt_rust dependency in Cargo.toml:
[dependencies]
rt_rust = { path = "PATH/TO/rust/rt-rust" }
Currently the macro-main library is not supported because it causes entry detection issues.