為嵌入式開發引入新的編程語言
對于嵌入式系統軟體和應用軟體的開發而言,編程語言的重要性次于整體的軟體架構設計的重要性,不過仍有必要謹慎地選擇編程語言,因為這涉及開發效率、軟體性能,以及團隊成員的技術能力,譬如,若整個嵌入式軟體的絕大陣列件是用C++撰寫的,那么招聘一些對C++不十分熟悉的開發人員,就是團隊建設的不合理了:精通C++的開發人員相對較少,對于小型的團隊,選擇一些不常用的編程語言,(短期內)不用考慮團隊建設的問題,同時可以很大程度上提升開發效率、減少軟體缺陷,提高軟體的可維護性、可擴展性,隨著嵌入設備的性能越來越高,可以在嵌入式設備上選擇Golang/NodeJS/MicroPython等高級的(腳本)編程語言來進行開發,Rust是一門新興的系統編程語言,強大的語言特性及良好的底層設計,使得它可以為Cortex-M系列的ARM單片機寫裸應用,可以在執行效上媲美C/C++,可以在面向物件編程方面接近Java,甚至目前開源社區正在努力將其整合到Linux內核中,作為Linux內核驅動開發的第二種編程語言,不過現在常用Rust語言開發的,可能在深度學習演算法和區塊鏈等領域,
本文記錄了而筆者搭建Rust交叉編譯環境的搭建程序,并在樹霉派設備上運行了第一個由Rust撰寫的Hello World程式,
為系統安裝Rust開發環境
筆者常用的系統為ubuntu,可以通過官方軟體源來安裝Rust開發環境:
sudo apt install build-essential rustc rust-doc cargo cargo-doc
安裝過后,可以通過瀏覽器打開/usr/share/doc/rust-doc/html/index.html檔案,包含了Rust的官方檔案,含有豐富的開發資料,不過這種方式安裝的Rust開發環境不能進行交叉編譯,缺少rustup命令列工具,該工具需要通過snap來安裝,但國內訪問snap源因網路問題很不容易,最終筆者選譯通過Rust官方推薦的安裝方式來安裝,
安裝的操作比較簡單,首先,以超級用戶執行:
export CARGO_HOME=/opt/rust-lang/cargo
export RUSTUP_HOME=/opt/rust-lang/rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > sh.rustup.rs
sh ./sh.rustup.rs
上面的兩行export指令指示cargo和rustup的安裝路徑,執行sh.rustup.rs腳本就會執行Rust開發環境的安裝,這是一個互動的安裝程序,當提示選擇時,選譯第一個選項,以下是安裝程序,根據網路環境的不同,可以會持續十幾分鐘:
root@rustling:~# sh ./sh.rustup.rs
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/opt/rust-lang/rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/opt/rust-lang/cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/opt/rust-lang/cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/root/.profile
/root/.bashrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2021-05-10, rust version 1.52.1 (9bc8c42bb 2021-05-09)
info: downloading component 'cargo'
6.0 MiB / 6.0 MiB (100 %) 2.7 MiB/s in 2s ETA: 0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
15.3 MiB / 15.3 MiB (100 %) 537.9 KiB/s in 21s ETA: 0s
info: downloading component 'rust-std'
24.1 MiB / 24.1 MiB (100 %) 1.9 MiB/s in 18s ETA: 0s
info: downloading component 'rustc'
48.4 MiB / 48.4 MiB (100 %) 50.3 KiB/s in 2m 7s ETA: 0s
info: downloading component 'rustfmt'
3.6 MiB / 3.6 MiB (100 %) 351.4 KiB/s in 16s ETA: 0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
15.3 MiB / 15.3 MiB (100 %) 3.7 MiB/s in 3s ETA: 0s
info: installing component 'rust-std'
24.1 MiB / 24.1 MiB (100 %) 7.9 MiB/s in 3s ETA: 0s
info: installing component 'rustc'
48.4 MiB / 48.4 MiB (100 %) 9.2 MiB/s in 5s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.52.1 (9bc8c42bb 2021-05-09)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory (/opt/rust-lang/cargo/bin).
To configure your current shell, run:
source /opt/rust-lang/cargo/env
為上面安裝的Rust開發環境增加交叉編譯支持
以上操作僅為開發系統安裝了Rust開發環境,要進行交叉編譯,仍需要以下操作:
source /opt/rust-lang/cargo/env
rustup target add aarch64-unknown-linux-gnu
rustup target add armv7-unknown-linux-gnueabihf
筆者安裝了兩個交叉編譯Rust的支持:32位ARM和64位ARM,不過仍需要安裝兩者的gcc交叉編譯器,因為Rust在構建時會呼叫aarch64-linux-gnu-gcc等工具做為聯結器,這個安裝程序不再贅述,可以自行安裝,以上操作完成后,就可以切換到普通用戶,進行第一個Rust應用的交叉編譯了,首先要配置環境變數:
export CARGO_HOME=/opt/rust-lang/cargo
export RUSTUP_HOME=/opt/rust-lang/rustup
export PATH=${CARGO_HOME}/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
export PATH=${PATH}:/opt/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin
之后,創建簡單的可執行應用,并在主機上編譯運行:
cargo new --bin hello
cd hello
cargo build
cargo run
以上操作的結果為:
yejq@rustling:~/hello$ cargo build
Compiling hello v0.1.0 (/home/yejq/hello)
Finished dev [unoptimized + debuginfo] target(s) in 7.73s
yejq@rustling:~/hello$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/hello`
Hello, world!
至此,為主機搭建的Rust開發環境功能正常,接下來需要為64位ARM交叉編譯該簡單應用,需要配置交叉編譯的聯結器:
mkdir ~/.cargo
cat <<EOF > ~/.cargo/config
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-none-linux-gnu-gcc"
EOF
cargo build --release --target=aarch64-unknown-linux-gnu
交叉編譯的結果如下:
yejq@rustling:~/hello$ cargo build --release --target=aarch64-unknown-linux-gnu
Compiling hello v0.1.0 (/home/yejq/hello)
Finished release [optimized] target(s) in 3.23s
yejq@rustling:~/hello$ ls ./target/
CACHEDIR.TAG aarch64-unknown-linux-gnu debug release
yejq@rustling:~/hello$ ls ./target/aarch64-unknown-linux-gnu/release/
build deps examples hello hello.d incremental
yejq@rustling:~/hello$ file ./target/aarch64-unknown-linux-gnu/release/hello
./target/aarch64-unknown-linux-gnu/release/hello: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, with debug_info, not stripped
將交叉編譯生成的可執行檔案hello復制到嵌入式ARM設備上運行,可以得到期望的結果:
# ./hello
Hello, world!
至此,為嵌入式設備搭建Rust交叉編譯開發環境就完成了,以后可以在嵌入式設備上用Rust編程語言來開發系統應用了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/286442.html
標籤:區塊鏈
上一篇:深入淺出區塊鏈,手把手帶你入門
