上一篇通过gn构建系统利用部件构建了可执行程序、动态库和配置文件,以及部件内的引用,本篇通过实现跨部件的模块引用,通过实现部件间的使用方法,以此来达到复用三方部件和模块库的目的。
本节以实现两个自定义的部件为例,compdemo3部件为demo3model1_lib模块的动态库部件,compdemo4部件为demo4model1_bin模块为可执行程序部件,以此来描述如何跨部件的引用。
示例部件compdemo3和部件compdemo4完整目录结构如下
├── componentdemo3
│ ├── BUILD.gn
│ ├── bundle.json
│ ├── include
│ │ └── model1.h
│ └── src
│ └── model1.cpp
└── componentdemo4
├── BUILD.gn
├── bundle.json
├── include
│ └── model1.h
└── src
└── model1.cpp
import("//build/ohos.gni") # 导入编译模板,编译环境等依赖
ohos_shared_library("demo3model1_lib"){ # 动态库模块
sources = [ # 模块源码
"src/model1.c"
]
include_dirs = [ # 模块依赖头文件
"include"
]
cflags = []
cflags_c = []
cflags_cc = []
ldflags = []
configs = []
deps = [] # 部件内部依赖
external_deps = [] # 跨部件的依赖,格式为"部件名:模块名"
part_name = "compdemo3" # 模块所属部件名称
}
注:复制代码时需要删除#及注释,否则无法编译通过
{
"name": "@ohos/compdemo3",
"description": "compdemo3 lib services",
"version": "3.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "my_test/componentdemo3"
},
"dirs": {},
"scripts": {},
"component": {
"name": "compdemo3",
"subsystem": "mycomptest",
"syscap": [],
"features": [],
"adapted_system_type": [ "mini", "small", "standard" ],
"rom": "10KB",
"ram": "10KB",
"deps": {
"components": [],
"third_party": [],
"hisysevent_config": []
},
"build": {
"sub_component": [
"//my_test/componentdemo3:demo3model1_lib"
],
"inner_kits": [ # 声明部件下那些模块允许在子系统内部使用
{
"type": "so" # 类型为so
"name": "//my_test/componentdemo3:demo3model1_lib" # 模块
"header": { # 头信息
"heads_files": [ # 头文件名称
"model1.h"
],
"header_base": "//my_test/componentdemo3/include" # 头文件base路径
}
}
],
"test": []
}
}
}
model1.h
#ifndef MODEL_1_H
#define MODEL_1_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
void HelloPrintModel1();
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif
model1.cpp
#include
#include "model1.h"
int main(int argc, char **argv)
{
HelloPrintModel1();
return 0;
}
void HelloPrintModel1()
{
printf("This component dir is /my_test/componentdemo3 mycomptest->compdemo3->demo3model1_lib\n");
}
import("//build/ohos.gni") # 导入编译模板,编译环境等依赖
ohos_executable("demo4model1_bin"){ # 可执行模块
sources = [ # 模块源码
"src/model.cpp"
]
include_dirs = [ # 模块依赖头文件
"include"
]
cflags = []
cflags_c = []
cflags_cc = []
ldflags = []
configs = []
deps = [] # 部件内部依赖
external_deps = [compdemo3:demo3model1_lib] # 跨部件的依赖,格式为"部件名:模块名",引入外部部件的模块
part_name = "compdemo4" # 模块所属部件名称
install_enable = true # 是否安装(缺省默认不安装)
}
注:复制代码时需要删除#及注释,否则无法编译通过
{
"name": "@ohos/compdemo4",
"description": "compdemo4 services",
"version": "3.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "my_test/componentdemo4"
},
"dirs": {},
"scripts": {},
"component": {
"name": "compdemo4",
"subsystem": "mycomptest",
"syscap": [],
"features": [],
"adapted_system_type": [ "mini", "small", "standard" ],
"rom": "10KB",
"ram": "10KB",
"deps": {
"components": ["compdemo3"], # 引入外部部件compdemo3
"third_party": [],
"hisysevent_config": []
},
"build": {
"sub_component": [
"//my_test/componentdemo4:demo4model1_bin"
],
"inner_kits": [],
"test": []
}
}
}
model.h
#ifndef MODEL_H
#define MODEL_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
void HelloPrint();
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif
model.cpp
#include
#include "model.h"
int main(int argc, char **argv)
{
HelloPrint();
return 0;
}
void HelloPrint()
{
printf("This component dir is /my_test/componentdemo4, mycomptest->compdemo4->demo4model_bin\n");
}
,
"mycomptest": {
"path": "my_test",
"name": "mycomptest"
}
...
}
# 注:以上...}代表内容放在文件最后一级}下
注:subsystem_config.json文件此处的"subsystem": "mycomptest"为bundle.json中的"subsystem": "mycomptest"。
,
{
"subsystem":"mycomptest",
"components": [
{
"component": "compdemo3",
"features": []
},
{
"component": "compdemo4",
"features": []
}
]
}
...
]
}
注:以上...]}代表以上内容放在文件中最后一级数组中
注:config.json文件中此处的"subsystem":"mycomptest"为subsystem_config.json文件中的"name": "mycomptest";"component": "compdemo3"和"component": "compdemo3"为BUILD.gn中的part_name = "compdemo3"和part_name = "compdemo4"。
可以使用整编命令
./build.sh --product-name rk3568 --no-prebuilt-sdk
也可以使用“–build-target 模块名"单独编译,编译命令如下:
./build.sh --product-name rk3568 --build-target demo4model1_bin --ccache
还可以编译模块所在的部件:
./build.sh --product-name rk3568 --build-target compdemo4 --ccache
10.烧录到设备后,输入模块命令即可打印
demo4model1_bin
import("//build/ohos.gni")
abc = true
bcd = "123"
...
if(abc == false){
print("This priint is gn key abc is false")
}
if(bcd == "123") {
print("This priint is gn key bcd is 123")
}
if(defined(abc)) { # 变量被定义
print("This priint is gn key abc define")
}
2.gn定义宏变量
import("//build/ohos.gni")
ohos_shared_library("demo3model1_lib"){
...
defines = ["SN"] # 定义宏变量方式1
cflags = ["-DUUID"] # 定义宏变量方式2 -DXXX,中间无空格
cflags_c = []
cflags_cc = []
ldflags = []
configs = []
...
}
注:复制代码时需要删除#及注释,否则无法编译通过
在c代码中使用
#include
#include "model1.h"
int main(int argc, char **argv)
{
HelloPrintModel1();
return 0;
}
void HelloPrintModel1()
{
#ifdef SN
printf("This define SN print");
#endif
#ifdef UUID
printf("This define UUID print");
#endif
printf("This component dir is /my_test/componentdemo3 mycomptest->compdemo3->demo3model1_lib\n");
}