概述
该程序是基于OpenHarmony标准系统编写的基础程序类:HelloWorld。
系统版本:openharmony5.0.0
开发板:dayu200
编译环境:ubuntu22
部署路径: //vendor/hihope/rk3568/samples
源码目录
创建//vendor/hihope/rk3568/samples/01_helloworld目录,并添加如下文件:
a01_helloworld
├── helloworld.c # .c源代码
├── BUILD.gn # GN文件
其中,BUILD.gn表示编译脚本,helloworld.c表示编译源文件。
创建BUILD.gn
编辑BUILD.gn文件。
import("//build/ohos.gni")
ohos_executable("helloworld") {
sources = [ "helloworld.c" ] # 参与编译的源代码文件
part_name = "helloworld" # 模块名称,需要确保在out/rk3568/build_configs/parts_info/part_subsystem.json文件中配置相对应的名称,否则编译报错([OHOS ERROR] Command: /usr/bin/env XX/oh5.0.0/build/templates/common/get_subsystem_name.py --part-name hello --part-subsystem-info-file build_configs/parts_info/part_subsystem.json)
install_enable = true # 安装到系统中
}
✒️注意:
(1)BUILD.gn中所有的TAB键必须转化为空格,否则会报错。如果自己不知道如何规范化,可以:
# 安装gn工具
sudo apt-get install ninja-build
sudo apt install generate-ninjas
# 规范化BUILD.gn
gn format BUILD.gn

(2)可执行程序的名称
ohos_executable("helloworld")
中的helloworld
为可执行程序的名称,表明OpenHarmony编译时将编译一个可执行程序helloworld。
创建源代码
helloworld.c具体代码如下:
c
#include <stdio.h>
#include <stdint.h>
int main(int argc,char *argv[]){
printf("\n");
printf("**********************************\n");
printf("\n");
printf("\tHello World!\n");
printf("\n");
printf("**********************************\n");
printf("\n");
printf("\n");
}
部署说明
首先,在ohos.build(即主编译文件)添加需要编译的目录samples。在//vendor/hihope/rk3568/ohos.build添加编译模块系统名称。
{
"parts": {
"product_rk3568": {
"module_list": [
"//vendor/hihope/rk3568/default_app_config:default_app_config",
"//vendor/hihope/rk3568/image_conf:custom_image_conf",
"//vendor/hihope/rk3568/preinstall-config:preinstall-config",
"//vendor/hihope/rk3568/resourceschedule:resourceschedule",
"//vendor/hihope/rk3568/etc:product_etc_conf",
"//vendor/hihope/rk3568/hals/audio:hdf_audio_config",
"//vendor/hihope/rk3568/hals/codec/:hdf_codec_config",
"//vendor/hihope/rk3568/hdf_config/uhdf:hdf_config",
"//vendor/hihope/rk3568/window_config:window_config",
"//vendor/hihope/rk3568/samples:samples"
]
}
},
"subsystem": "product_hihope"
}
在samples/BUILD.gn文件添加一行编译引导语句。
import("//build/ohos.gni")
group("samples") {
deps = [
"01_helloworld:helloworld",
]
}
"01_helloworld:helloworld",
该行语句表示引入//vendor/hihope/rk3568/samples/01_helloworld参与编译。
编译命令
./build.sh --product-name rk3568 --build-target helloworld
验证是否成功:

手动传输
将执行文件拷贝出来并放到终端中
从虚拟机中拷出执行文件(helloworld)
zcc@ubuntu22:~/oh5.0.0$ sz out/rk3568/exe.unstripped/applications/prebuilt_hap/helloworld
通过hdc工具拷贝至终端中
hdc.exe file send .\helloworld /data/local/tmp/

运行程序
系统启动后,运行命令:
