更多信息请访问:与华为官方共同建立的鸿蒙技术社区https://harmonyos.51cto.com背景:离不开项目开发沟通请求来自http消息的组装和发送。所以,合理的使用cjson来拼装json文件格式,往往会降低开发的难度。本文将详细讲解openharmony第三方CJSON动态库的编译过程。由于环境限制,本次编译编译开发环境为君正x2000L1级开发板,非L2级。环境开发板:Kimiseix2000,L1级编辑示例如下:#include#include#include#include"cJSON.h"voidsetsysjson(void){cJSON*根,*正文,*列表;root=cJSON_CreateObject();cJSON_AddItemToObject(root,"sysstatis",body=cJSON_CreateArray());cJSON_AddItemToArray(body,list=cJSON_CreateObject());,"abc");cJSON_AddStringToObject(列表,"主机名","abc1");cJSON_AddStringToObject(列表,“发布”,“abc2”);cJSON_AddStringToObject(列表,“版本”,“abc3”);“机器”,“abc4”);char*s=cJSON_PrintUnformatted(root);如果(s){printf("%s\n",s);免费;}if(root){cJSON_Delete(root)}return;}intmain(intargc,char**argv){setsysjson();return0;}BUILD.gn语句内容import("//build/lite/config/component/lite_component.gni")executable("cjson_test"){sources=["./test.c",]include_dirs=["//third_party/cJSON",]cflags=["-Wall","-Wno-format","-Wwrite-strings",]cflags_cc=cflagsldflags=["-lstdc++","-lpthread"]public_deps=["//build/lite/config/component/cJSON:cjson_shared",]deps=[#"//third_party/cJSON:cjson_shared"#"//third_party/cJSON:cjson"]}group("sys_info"){deps=[":cjson_test",]}放入代码中执行,生成对应的可执行文件:lts@ubuntu:~/x2000$find./-namecjson_test./out/x2000/halley5_spi_nand_flash/unstripped/bin/cjson_test./out/x2000/halley5_spi_nand_flash/rootfs/bin/cjson_test./out/x2000/halley5_spi_nand_flash/bin/cjson_testlts@ubuntu:~/x2000$上传到开发板:运行结果:开发笔记问题一:[OHOS错误]../../../talkweb/sysinfo/test.c:4:10:致命错误:cJSON.h:没有这样的文件或目录[OHOSERROR]#include"cJSON.h"[OHOSERROR]^~~~~~~~~[OHOSERROR]编译终止。[OHOSERROR]你可以检查构建日志/home/lts/x2000/out/x2000/halley5_spi_nand_闪存/构建.log[OHOSERROR]命令:“/home/lts/x2000/prebuilts/build-tools/linux-x86/bin/ninja-wdupbuild=warn-C/home/lts/x2000/out/x2000/halley5_spi_nand_flash”失败[OHOSERROR]返回码:1[OHOSERROR]执行路径:/home/lts/x2000没有指定头文件,即在include_dirs添加对应用头文件include_dirs=["//third_party/cJSON",]问题二[OHOSINFO][696/3910]行动//test/xts/acts/build_lite:acts_xdevice(//build/lite/toolchain:mips-linux-gnu-gcc)[OHOS错误][687/3910]LINK./bin/cjson_test[OHOS错误]失败:bin/cjson_testunstripped/bin/cjson_test[OHOS错误]ccache/home/lts/x2000/device/ingenic/x2000/prebuilt/gcc/linux-x86/mips/mips-gcc720-glibc229/bin/mips-linux-gnu-gcc-lstdc++-lpthread-Wl,-rpath-link=/home/lts/x2000/out/x2000/halley5_spi_nand_flash/libs/lib,-rpath-link=/home/lts/x2000/out/x2000/halley5_spi_nand_flash,-rpath-link=/home/lts/x2000/out/x2000/halley5_spi_nand_flash/buildroot-intermediate/target/usr/lib-latomic-lpthread-ldl-lrt-mips32r2-mfp64-fpic-D_GNU_SOURCE-march=mips32r2-L。-Wl,-z,now-Wl,-z,relro-Wl,-z,noexecstack-pie-Wl,--start-groupobj/talkweb/sysinfo/cjson_test.test.o-Wl,--end-group-o./unstripped/bin/cjson_test&&ccache/home/lts/x2000/device/ingenic/x2000/prebuilt/gcc/linux-x86/mips/mips-gcc720-glibc229/bin/mips-linux-gnu-strip--strip-unneeded"./unstripped/bin/cjson_test"-o"./bin/cjson_test"[OHOSERROR]obj/talkweb/sysinfo/cjson_test.test.o:在函数`setsysjson':[OHOSERROR]测试中。c:(.text+0x30):未定义引用`cJSON_CreateObject'[OHOSERROR]test.c:(.text+0x38):未定义引用`cJSON_CreateObject'[OHOSERROR]test.c:(.text+0x44):对`cJSON_CreateArray'[OHOSERROR]的未定义引用OHOSERROR]test.c:(.text+0x68):undefinedreferenceto`cJSON_AddItemToObject'[OHOSERROR]test.c:(.text+0x74):未定义引用`cJSON_CreateObject'[OHOSERROR]test.c:(.text+0x78):未定义引用`cJSON_CreateObject'[OHOSERROR]test.c:(.text+0x8c):未定义引用`cJSON_AddItemToArray'[OHOSERROR]test.c:(.text+0x90):undefinedreferenceto`cJSON_AddItemToArray'[OHOSERROR]test.c:(.text+0xa8):undefinedreferenceto`cJSON_AddStringToObject'[OHOSERROR]测试.c:(.text+0xb0):未定义引用`cJSON_AddStringToObject'[OHOSERROR]test.c:(.text+0xc8):未定义引用`cJSON_AddStringToObject'[OHOSERROR]test.c:(.text+0xd0):未定义对`cJSON_AddStringToObject'的引用[OHOSERROR].text+0xf0):更多未定义的对`cJSON_AddStringToObject'的引用遵循[OHOSERROR]obj/talkweb/sysinfo/cjson_test.test.o:Infunction`setsysjson':[OHOSERROR]test.c:(.text+0x13c):未定义的引用ceto`cJSON_PrintUnformatted'[OHOSERROR]test.c:(.text+0x140):undefinedreferenceto`cJSON_PrintUnformatted'[OHOSERROR]test.c:(.text+0x1b4):undefinedreferenceto`cJSON_Delete'[OHOSERROR]]]test.c:(.text+0x1bc):undefinedreferenceto`cJSON_Delete'[OHOSERROR]collect2:error:ldreturned1exitstatus[OHOSERROR]你可以检查构建登录/home/lts/x2000/out/x2000/halley5_spi_nand_flash/build.log[OHOSERROR]命令:“/home/lts/x2000/prebuilts/build-tools/linux-x86/bin/ninja-wdupbuild=warn-C/home/lts/x2000/out/x2000/halley5_spi_nand_flash"failed[OHOSERROR]返回码:1[OHOSERROR]执行路径:/home/lts/x2000解决方法:需要指定对应的cjson库注意我之前测试过使用deps做依赖,但是还是出现同样的问题deps=["//third_party/cJSON:cjson_shared""//third_party/cJSON:cjson"]编译还是报错。然后使用public_deps依赖项来避免编译错误。public_deps=["//build/lite/config/component/cJSON:cjson_shared",]综上所述,在使用系统自带的第三方库时,可以参考上面的开发陷阱,尝试使用public_deps来避免依赖问题。当然是自己开发的动态库,不系统的可以参考https://harmonyos.51cto.com/posts/10353。更多信息请访问:鸿蒙科技与华为官方社区共建https://harmonyos.51cto.com