编写helloworld
- 代码根目录创建sample子系统文件夹
- 在子系统目录下创建hello部件文件夹
- hello文件夹中创建hello源码目录及源码
sample/hello/src/helloworld.c:
#include <stdio.h>
#include "helloworld.h"
void hello_oh(void);
int main(int argc, char *argv[])
{
hello_oh();
HelloWorld();
return 0;
}
void HelloWorld(void)
{
printf("\n\n");
printf("Hello World!\n");
printf("\n\n");
}
void hello_oh(void)
{
printf("\n\n");
printf("Hello OpenHarmony!\n");
printf("\n\n");
}
sample/hello/include/helloworld.h:
#ifndef __HELLOWORLD_H__
#define __HELLOWORLD_H__
#ifdef __cplusplus
extern "C" {
#endif
void HelloWorld();
#ifdef __cplusplus
}
#endif
#endif
- 构建文件BUILD.gn及部件配置文件bundle.json。
sample/hello/BUILD.gn:
import("//build/ohos.gni") # 导入编译模板
ohos_executable("helloworld") { # 可执行模块
sources = [ # 模块源码
"src/helloworld.c"
]
include_dirs = [ # 模块依赖头文件目录
"include"
]
cflags = []
cflags_c = []
cflags_cc = []
ldflags = []
configs = []
deps =[] # 部件内部依赖
part_name = "hello" # 所属部件名称,必选
install_enable = true # 是否默认安装(缺省默认不安装),可选
}
sample/hello/bundle.json:
{
"name": "@ohos/hello",
"description": "Hello world example.",
"version": "3.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "sample/hello"
},
"dirs": {},
"scripts": {},
"component": {
"name": "hello",
"subsystem": "sample",
"syscap": [],
"features": [],
"adapted_system_type": [ "mini", "small", "standard" ],
"rom": "10KB",
"ram": "10KB",
"deps": {
"components": [],
"third_party": []
},
"build": {
"sub_component": [
"//sample/hello:helloworld"
],
"inner_kits": [],
"test": []
}
}
}
- 修改子系统配置文件
build/subsystem_config.json:
"sample": {
"path": "sample",
"name": "sample"
},
添加自定义的sample子系统
- 修改产品配置文件
vendor/hihope/rk3568/config.json:
{
"subsystem": "sample",
"components": [
{
"component": "hello",
"features": []
}
]
},
在产品选中加入自定义的子系统。
最后整个sample子系统目录结构如下:
sample/
└── hello
├── BUILD.gn
├── bundle.json
├── include
│ └── helloworld.h
└── src
└── helloworld.c
编译
指定编译helloworld模块:
./build.sh --product-name rk3568 --ccache --build-target helloworld
编译完成后目标文件所在位置:out/rk3568/sample/hello/helloworld
编译好的程序默认安装在system分区中:
system/system/bin/helloworld
每编译一次镜像太久了,就算是单独编译镜像也要很久:
./build.sh --product-name rk3568 --ccache --build-target system_image
为了快速验证,直接将编译的helloworld拷贝进镜像里:
mkdir system
sudo mount system.img system
sudo cp ../../../sample/hello/helloworld system/system/bin/
sudo umount system
hdc shell连接调试终端,执行helloworld: