Supporting Multiple Crates
less than a minute
Limitations
During the development of XWOS Rust, it became necessary to separate library and application code into different crates. In practice, the following Rust limitations were discovered:
- Two crates cannot both be compiled as staticlib. A staticlib crate cannot be listed in
[dependencies], indicating that staticlib is meant to be called by C/C++; - Only rlib crates can be listed in
[dependencies]. When compiling staticlib, all crates in[dependencies]will be packaged within the staticlib; - When linking with C/C++ code, there cannot be two or more staticlibs, unless these staticlibs’ dependencies are completely unrelated (which is difficult to achieve in practice), otherwise duplicate definition linker errors will occur.
Solution
Through practice, the following solution was developed:
- Move
xwrust_main()into a board modulebm/rustapp, which is compiled as staticlib; - All Rust applications can only be concentrated within rustapp;
- Other crates only serve as
[dependencies]of rustapp; - Other crates are set to
crate-type = ["rlib"], andxwmo.mkis deleted, no longer using XWOS’s build system for compilation.
Experimental Code
- Code repository:
git clone --recursive https://gitee.com/xwos/WeActMiniStm32H750.git - commit
cd XWOS
git pull
git checkout -b rust-multicrates 4d0837bfe227a6316288e4f1fa60a15276504159