当前位置: 首页 > 科技观察

helloworld(L2)

时间:2023-03-16 17:28:12 科技观察

用于Openharmony设备开发更多信息请访问:Harmonyos.51cto.com,介绍,子系统添加,静态库编译,动态库编译,动态库和静态库调用,设备开发:partA/feature1编译静态库,partB/module编译动态库partA/feature2在可执行程序中调用动态库和静态库2.添加编译代码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.gnimport("//build/ohos.gni")config("helloworld1_lib_config"){include_dirs=["include"]}ohos_static_library("libhelloworld1_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/模块目录结构配置文件test\example\partB\module\BUILD.gnimport("//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.gnimport("//build/ohos.gni")ohos_executable("helloworld2_bin"){sources=["src/helloworld2.c"]include_dirs=["include","//test/example/partB/module/include"]deps=[#组件中的模块依赖"../feature1:libhelloworld1_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"//modulepartA/feature1#include"module.h"//模块partB/module#includevoidhelloworld2(void){printf("[demo]helloworld2222\n");helloworld1();//partA/feature1模块();//partB/module}intmain(){helloworld2();return0;}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”:“所以","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--亲duct-nameHi3516DV300--ccache--build-targethelloworld2_bin编译成功后,可以使用hdc_std.exe将编译好的helloworld2_bin和libmodule_lib.z.so发送到Hi3516DV300开发板运行,并将调用结果输出到串口终端3.2修改系统权限,可读写目录:mount-oremount,rw/3.3发送文件到开发板:hdc_std.exefilesendZ:\L2\out\ohos-arm-发布\sub_example\partB\libmodule_lib.z。所以/system/lib//开发板目录/data/test为自建目录,如果没有,先创建。hdc_std.exe文件发送Z:\L2\out\ohos-arm-release\sub_example\partA\helloworld2_bin/data/test3.3修改后可执行权限:chmod0711/data/test/helloworld2_bin3.4运行:/data/test/helloworld2_bin文件中的代码未完全显示。下载【源码】重点目录:example\partB\module,example\partA\feature1,example\partA\feature2基本结构保持不变。更多信息请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com