OpenHarmony4.1上初体验rust

OpenHarmony4.1上初体验rust


前言

由于后续可能要做rust基于OpenHarmony的开发工作,于是先开始一些调研性的工作,rust了解的内容可以参考该文章:
ubuntu20.04搭建RUST开发环境并与C语言交互


一、准备工作

OpenHarnmony4.1版本,有设备的话可以用riscv的荔枝派,rk3568的dayu系列开发板,没有设备的话可以用QEMU。

本文以下的尝试均基于riscv的OpenHarnmony4.1版本在荔枝派上进行。

整编OH版本并烧录至荔枝派设备,确保可以正常启动桌面。

二、初步调研

rust相关的目录主要有以下几个:

  1. build\rust:有一些rust相关的测试demo,比如和c、c++代码进行互调的测试等,可以使用以下命令进行编译,其中build-target可以使用build\rust\tests\BUILD.gn中的group。

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target ohos_rust_xxxtest_tests

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target cargo_crate_test

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target cxx_test_host_and_target

  1. third_party\rust\crates 里面东西很多,需要慢慢看。
  2. build\templates\rust
  3. prebuilts\rustc-riscv
  4. 其他的发现以后再进行补充。

三、初步尝试

运行测试demo

使用上面的编译命令将相关的demo编译出后通过HDC命令将相关的进程和库文件拷贝至荔枝派设备的"/data "目录。

其中编译ohos_rust_xxxtest_tests 目标时会有如下报错:

应该是编译器限制的比较严格,不能有warning导致,如下修改代码即可

build\rust\tests\test_proc_macro_crate\test_derive_macro\src\main.rs

c 复制代码
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

extern crate derive_macro;
use derive_macro::AnswerFn;

#[derive(AnswerFn)]
#[allow(dead_code)] //新增此行
struct Struct;

fn main() {
    assert_eq!(42, answer());
}

生成的相关二进制位于out/build/build_framework/

在荔枝派上./运行,除了一个需要设置一下环境变量,其他都可以正常跑出结果。

export LD_LIBRARY_PATH=/data/build_framework

四、尝试构建自己的hello world

就在build/rust目录下开始编写自己的hello world。

  1. 新建目录,目录下新增main.rsbuild.rsBUILD.gn
    main.rs就只有一个println!,build.rs从test的其他demo直接拿过来用,BUILD.gn也可以仿照其他的demo。
  2. 修改build目录下的bundle.json,增加新的build-target。运行编译命令(其中rust_liusai为我新添加到bundle.json的group):

./build.sh --product-name rvbook --no-prebuilt-sdk --build-target rust_liusai

原因是"-D missing-docs",找一下解决方法。

把编译命令中的"-D missing-docs"直接删除,然后cd到目录out/rvbook编译,成功得到进程文件。HDC到设备上./运行:

成功。

五、与C、C++的交互

参考OH自带的文档

third_party\rust\crates\cxx\readme_zh.md

third_party\rust\crates\bindgen\readme_zh.md

docs\zh-cn\device-dev\subsystems\subsys-build-rust-compilation.md

明天搞出来以后进行补充。

总结

需要了解OopenHarmony,HDC,ninja编译体系构建。

熟悉rust语言,rust编译。

相关推荐
少一倍的优雅2 小时前
hi3863(WS63) 智能小车 (一) 简单介绍
单片机·嵌入式硬件·harmonyos·hi3863
卡奥斯开源社区官方3 小时前
鸿蒙智行 L3 内测启幕:从技术架构到商用落地的全链路技术拆
华为·架构·harmonyos
DuanPenghao3 小时前
RISCV实战:实现基于Verilator模拟蜂鸟E203的加法器和卷积神经网络仿真
人工智能·嵌入式硬件·神经网络·cnn·risc-v
搬砖的kk3 小时前
Flutter UUID 鸿蒙平台适配实践 - 全版本测试与验证
flutter·华为·harmonyos
简单点好不好4 小时前
2025-简单点-从0开始学Rust-更新
rust
梧桐ty4 小时前
硬件交互联动:基于鸿蒙的Flutter物联网应用开发实战
flutter·华为·harmonyos
鸿蒙小白龙4 小时前
鸿蒙UniProton操作系统编译开发指导
harmonyos·鸿蒙系统·openharmony·uniproton
萌虎不虎4 小时前
【鸿蒙根据图片路径读取图片的base64数据】
华为·harmonyos
梧桐ty6 小时前
鸿蒙生态下的跨平台框架选型指南:Flutter vs React Native vs uni-app
flutter·华为·harmonyos
码力码力我爱你7 小时前
HarmonyOS DevEco Studio的使用练习题
华为·harmonyos