如果只是想简单的增加代码到OpenHarmony系统,可以添加到现成子系统的部件中。以下是实现过程:
1. 确认产品配置中使用的子系统与部件
如Hi3516的ipcamera_hispark_taurus:
图中内容表示已使用"application" 子系统,以及它的"camera_sample_app"、"camera_screensaver_app"部件。也就说可以将代码添加到"application"子系统的任意一个部件中,这里选择"camera_sample_app"部件。
2. 自定义程序
在/opt/Ohos4/OpenHarmony/applications/sample/camera目录下创建mytest文件夹,并在mytest目录下创将myhello.c源文件。源文件内容如下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *threadFunc(void *arg);
int main(void)
{
pthread_t ptid;
int n = 0;
printf("hello pthread test\n");
pthread_create(&ptid, NULL, threadFunc, NULL);
while (1)
{
printf("in main thread : %d\n", n++);
sleep(5);
}
return 0;
}
void *threadFunc(void *arg)
{
int n = 0;
while (1)
{
printf("in sub thread : %d\n", n++);
sleep(3);
}
}
3. 编译配置
在mytest目录下创建BUILD.gn, 并输入内容如下:
import ("//build/ohos.gni")
ohos_executable("mytest") {
sources = ["myhello.c"]
include_dirs = []
part_name = "camera_sample_app"
}
然后修改OpenHarmony/applications/sample/camera/bundle.json配置文件,增加并修改:
"build": {
"sub_component": [
"//applications/sample/camera/launcher:launcher_hap",
"//applications/sample/camera/cameraApp:cameraApp_hap",
"//applications/sample/camera/setting:setting_hap",
"//applications/sample/camera/gallery:gallery_hap",
"//applications/sample/camera/media:media_sample", #modify
"//applications/sample/camera/mytest:mytest" #add
],
"inner_kits": [],
"test": []
}
4. 执行
重新编译OpenHarmony系统后,如果开发板已烧录过系统镜像,则可以只再更新烧录rootfs_vfat.img文件即可,因自定义的程序是属于用户态空间。烧录完成后,在终端进入bin目录下执行mytest,输出信息: