Openharmony 设备开发之helloworld (L2)

系统 OpenHarmony
本篇分别从介绍子系统添加,介绍静态库编译,介绍动态库编译,介绍动态库和静态库的调用四个方面入门了解设备开发。

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

一、简介

  1. 介绍子系统添加
  2. 介绍静态库编译
  3. 介绍动态库编译
  4. 介绍动态库和静态库的调用

入门了解设备开发:

partA/feature1编译的静态库,

partB/module编译的是动态库

partA/feature2可执行程序中调用动态库和静态库

二、代码添加编译

2.1 子系统添加

配置文件:build/subsystem_config.json

,
"sub_example": {
"project": "hmf/test",
"path": "test/example",
"name": "sub_example",
"dir": "test"
}

如果自己想自定义目录,test为测试代码放在目录路径。

2.2 子模块添加

配置文件:productdefine/common/products/Hi3516DV300.json

{
"product_name": "Hi3516DV300",
"product_company": "hisilicon",
"product_device": "hi3516dv300",
"version": "2.0",
"type": "standard",
"product_build_path": "device/hisilicon/build",
"parts":{

"sub_example:partB":{},
"sub_example:partA":{}
}
}

2.3 模块partA/feature1

目录结构

编译配置文件:test\example\partA\feature1\BUILD.gn

import("//build/ohos.gni")

config("helloworld1_lib_config") {
include_dirs = [ "include" ]
}
ohos_static_library("libhelloworl1_lib") {
output_extension = "a"
sources = [
"include/helloworld1.h",
"src/helloworld1.c"
]
public_configs = [ ":helloworld1_lib_config" ]
part_name = "partA"
}

其中ohos_static_library标准系统是ninja生成静态库的关键。

2.4 模块partB/module

目录结构

配置文件test\example\partB\module\BUILD.gn

import("//build/ohos.gni")

config("module_lib_config") {
include_dirs = [ "include" ]
}

ohos_shared_library("module_lib") {
sources = [
"//test/example/partB/module/include/module.h",
"//test/example/partB/module/src/module.c"
]
public_configs = [ ":module_lib_config" ]
part_name = "partB"
subsystem_name = "sub_example"
}

其中ohos_shared_library标准系统是ninja生成动态库的关键。

2.5 动态库和静态库调用模块partA/feature2

目录结构

编译配置:test\example\partA\feature2\BUILD.gn

import("//build/ohos.gni")

ohos_executable("helloworld2_bin") {
sources = [
"src/helloworld2.c"
]
include_dirs = [
"include",
"//test/example/partB/module/include"
]
deps = [ # 组件内模块依赖
"../feature1:libhelloworl1_lib",
#"//test/example/partB/module:module_lib",
"../feature3:feature3_etc",
]
external_deps = [ "partB:module_lib", ] # 跨组件的依赖,格式为“组件名:模块名”
install_enable = true # 可执行程序缺省不安装,需要安装时需要指定
part_name = "partA"
subsystem_name = "sub_example"
}

调用的C代码:test\example\partA\feature2\src\helloworld2.c

#include "helloworld1.h" // 模块partA/feature1
#include "module.h" // 模块partB/module
#include <stdio.h>

void helloworld2(void)
{
printf("[demo] hello world 2222\n");
helloworld1(); // partA/feature1
module(); // partB/module
}

int main()
{
helloworld2();
return 0;
}

2.6 编译配置test\example\ohos.build

配置中的inner_kits是test\example\partA\feature2\BUILD.gn跨组件依赖配置的关键。

{
"subsystem": "sub_example",
"parts": {
"partB": {
"module_list": [
"//test/example/partB/module:module_lib"
],
"inner_kits": [
{
"type": "so",
"name": "//test/example/partB/module:module_lib",
"header": {
"header_files": [
"module.h"
],
"header_base": "//test/example/partB/module/include"
}
}
],
"system_kits": [],
"test_list": []
},
"partA": {
"module_list": [
"//test/example/partA/feature1:libhelloworl1_lib",
"//test/example/partA/feature2:helloworld2_bin"
],
"inner_kits": [],
"system_kits": [],
"test_list": []
}
}
}

三、编译测试运行

3.1 编译:

./build.sh --product-name Hi3516DV300 --ccache --build-target helloworld2_bin

编译成功后,可以把编译好的helloworld2_bin和libmodule_lib.z.so用hdc_std.exe发送到Hi3516DV300开发板中去运行,在串口终端上输出调用结果。

3.2 修改系统权限,目录能读能写:

mount -o remount,rw /

3.3 发送文件到开发板:

hdc_std.exe file send Z:\L2\out\ohos-arm-release\sub_example\partB\libmodule_lib.z.so /system/lib
//开发板目录/data/test为自建目录,没有的话,先创建。
hdc_std.exe file send Z:\L2\out\ohos-arm-release\sub_example\partA\helloworld2_bin /data/test

3.3 修改成可执行权后:

chmod 0711 /data/test/helloworld2_bin

3.4 运行:

/data/test/helloworld2_bin

 

文档中的代码没有完全展示,下载​​【源代码】​

重点关注目录:example\partB\module,example\partA\feature1,example\partA\feature2

代码库中的源码相对于文档中的代码有少许调整,基本结构不变.

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-02-17 16:47:40

OpenharmonIPC通信鸿蒙

2022-06-22 09:14:23

事件打点HiSysEvent

2022-02-15 14:06:36

OpenHarmon操作系统鸿蒙

2022-07-14 19:03:33

IPC服务鸿蒙

2022-07-04 16:41:16

IPC通信HiTrace

2022-04-06 11:27:05

harmonyeTS 开发NAPI开发

2014-07-30 16:43:49

Android

2022-02-17 17:52:00

openharmon项目开发鸿蒙

2021-10-20 19:14:30

缓存CacheCPU

2015-01-20 13:19:52

OpenStack网络层数据链路层

2022-03-21 15:42:36

智能家居物联网MQTT

2022-09-07 15:35:49

设备开发鸿蒙

2022-01-06 16:16:21

鸿蒙HarmonyOS应用

2022-07-29 14:29:24

设备开发鸿蒙

2022-06-14 15:07:04

IPC客户端服务端

2022-02-14 13:52:04

OpenHarmor系统鸿蒙

2022-10-24 14:54:29

LWIP协议鸿蒙

2023-01-31 09:12:16

CPU芯片缓存

2023-02-20 08:00:00

2022-10-25 14:51:11

设备开发鸿蒙
点赞
收藏

51CTO技术栈公众号