https://harmonyos.51cto.com
背景:
项目开发通信请求离不开http 报文的组装发送。因此合理利用cjson进行json文件格式的组装往往会降低开发难度。本文将详细讲解openharmony third_party CJSON 动态库编译过程。由于环境限制本次编译开发环境为君正x2000 L1 级别开发板非L2级别。
环境
开发板:君正x2000,L1级
编译
测试用例代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"
void setsysjson(void)
{
cJSON *root, *body, *list;
root = cJSON_CreateObject();
cJSON_AddItemToObject(root,"sysstatis", body = cJSON_CreateArray());
cJSON_AddItemToArray(body, list = cJSON_CreateObject());
cJSON_AddStringToObject(list,"system name","abc");
cJSON_AddStringToObject(list,"host name","abc1");
cJSON_AddStringToObject(list,"releases","abc2");
cJSON_AddStringToObject(list,"version","abc3");
cJSON_AddStringToObject(list,"machie","abc4");
char *s = cJSON_PrintUnformatted(root);
if(s) {
printf(" %s \n",s);
free(s);
}
if(root) {
cJSON_Delete(root);
}
return;
}
int main(int argc,char **argv)
{
setsysjson();
return 0;
}
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 = cflags
ldflags = [
"-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 ./ -name cjson_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_test
lts@ubuntu:~/x2000$
上传至开发板:
运行结果:
开发注意事项
问题一:
[OHOS ERROR] ../../../talkweb/sysinfo/test.c:4:10: fatal error: cJSON.h: No such file or directory
[OHOS ERROR] #include "cJSON.h"
[OHOS ERROR] ^~~~~~~~~
[OHOS ERROR] compilation terminated.
[OHOS ERROR] you can check build log in /home/lts/x2000/out/x2000/halley5_spi_nand_flash/build.log
[OHOS ERROR] command: "/home/lts/x2000/prebuilts/build-tools/linux-x86/bin/ninja -w dupbuild=warn -C /home/lts/x2000/out/x2000/halley5_spi_nand_flash" failed
[OHOS ERROR] return code: 1
[OHOS ERROR] execution path: /home/lts/x2000
没有指定头件,即在include_dirs 添加对应头文件
include_dirs = [
"//third_party/cJSON",
]
问题二
[OHOS INFO] [696/3910] ACTION //test/xts/acts/build_lite:acts_xdevice(//build/lite/toolchain:mips-linux-gnu-gcc)
[OHOS ERROR] [687/3910] LINK ./bin/cjson_test
[OHOS ERROR] FAILED: bin/cjson_test unstripped/bin/cjson_test
[OHOS ERROR] 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-group obj/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"
[OHOS ERROR] obj/talkweb/sysinfo/cjson_test.test.o: In function `setsysjson':
[OHOS ERROR] test.c:(.text+0x30): undefined reference to `cJSON_CreateObject'
[OHOS ERROR] test.c:(.text+0x38): undefined reference to `cJSON_CreateObject'
[OHOS ERROR] test.c:(.text+0x44): undefined reference to `cJSON_CreateArray'
[OHOS ERROR] test.c:(.text+0x48): undefined reference to `cJSON_CreateArray'
[OHOS ERROR] test.c:(.text+0x60): undefined reference to `cJSON_AddItemToObject'
[OHOS ERROR] test.c:(.text+0x68): undefined reference to `cJSON_AddItemToObject'
[OHOS ERROR] test.c:(.text+0x74): undefined reference to `cJSON_CreateObject'
[OHOS ERROR] test.c:(.text+0x78): undefined reference to `cJSON_CreateObject'
[OHOS ERROR] test.c:(.text+0x8c): undefined reference to `cJSON_AddItemToArray'
[OHOS ERROR] test.c:(.text+0x90): undefined reference to `cJSON_AddItemToArray'
[OHOS ERROR] test.c:(.text+0xa8): undefined reference to `cJSON_AddStringToObject'
[OHOS ERROR] test.c:(.text+0xb0): undefined reference to `cJSON_AddStringToObject'
[OHOS ERROR] test.c:(.text+0xc8): undefined reference to `cJSON_AddStringToObject'
[OHOS ERROR] test.c:(.text+0xd0): undefined reference to `cJSON_AddStringToObject'
[OHOS ERROR] test.c:(.text+0xe8): undefined reference to `cJSON_AddStringToObject'
[OHOS ERROR] obj/talkweb/sysinfo/cjson_test.test.o:test.c:(.text+0xf0): more undefined references to `cJSON_AddStringToObject' follow
[OHOS ERROR] obj/talkweb/sysinfo/cjson_test.test.o: In function `setsysjson':
[OHOS ERROR] test.c:(.text+0x13c): undefined reference to `cJSON_PrintUnformatted'
[OHOS ERROR] test.c:(.text+0x140): undefined reference to `cJSON_PrintUnformatted'
[OHOS ERROR] test.c:(.text+0x1b4): undefined reference to `cJSON_Delete'
[OHOS ERROR] test.c:(.text+0x1bc): undefined reference to `cJSON_Delete'
[OHOS ERROR] collect2: error: ld returned 1 exit status
[OHOS ERROR] you can check build log in /home/lts/x2000/out/x2000/halley5_spi_nand_flash/build.log
[OHOS ERROR] command: "/home/lts/x2000/prebuilts/build-tools/linux-x86/bin/ninja -w dupbuild=warn -C /home/lts/x2000/out/x2000/halley5_spi_nand_flash" failed
[OHOS ERROR] return code: 1
[OHOS ERROR] execution path: /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.