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编译。

相关推荐
VinciYan4 小时前
Rust使用Actix-web和SeaORM库开发WebAPI通过Swagger UI查看接口文档
rust·api·web·orm
白总Server6 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
冯志浩9 小时前
Harmony NEXT:如何给数据库添加自定义分词
harmonyos·掘金·金石计划
爱桥代码的程序媛11 小时前
鸿蒙OpenHarmony【轻量系统芯片移植案例】标准系统方案之瑞芯微RK3568移植案例
嵌入式硬件·harmonyos·鸿蒙·鸿蒙系统·移植·openharmony·鸿蒙开发
AORO_BEIDOU11 小时前
防爆手机+鸿蒙系统,遨游通讯筑牢工业安全基石
5g·安全·智能手机·信息与通信·harmonyos
新知图书12 小时前
Rust编程的作用域与所有权
开发语言·后端·rust
小强在此1 天前
【基于开源鸿蒙(OpenHarmony)的智慧农业综合应用系统】
华为·开源·团队开发·智慧农业·harmonyos·开源鸿蒙
许野平1 天前
Rust: Warp RESTful API 如何得到客户端IP?
tcp/ip·rust·restful·ip地址
PlumCarefree1 天前
基于鸿蒙API10的RTSP播放器(四:沉浸式播放窗口)
华为·harmonyos