Openharmony L1级设备之CJSON库调用

原创
系统 OpenHarmony
本文将详细讲解openharmony third_party CJSON 动态库编译过程。由于环境限制本次编译开发环境为君正x2000 L1 级别开发板非L2级别。

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

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

​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.

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

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

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

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

2022-03-28 15:40:34

harmony鸿蒙操作系统

2023-02-28 15:49:09

鸿蒙应用开发

2022-02-21 15:38:57

Openharmon操作系统鸿蒙

2021-10-20 19:14:30

缓存CacheCPU

2012-10-09 10:57:57

光纤iODN宽带

2022-06-22 09:14:23

事件打点HiSysEvent

2023-01-31 09:12:16

CPU芯片缓存

2022-03-21 15:42:36

智能家居物联网MQTT

2023-10-10 15:33:55

机器学习相似性度量

2022-01-07 21:12:46

鸿蒙HarmonyOS应用

2022-08-26 14:58:43

区块链比特币架构

2022-05-20 10:56:54

AbilityeTS FA调用

2022-09-07 15:35:49

设备开发鸿蒙

2023-11-08 15:51:00

AGI人工智能

2022-02-17 16:47:40

OpenharmonIPC通信鸿蒙

2022-07-29 14:29:24

设备开发鸿蒙

2024-07-12 12:51:44

2021-07-08 09:42:04

鸿蒙HarmonyOS应用

2022-10-24 14:54:29

LWIP协议鸿蒙

2022-02-24 16:00:59

Ability鸿蒙JS
点赞
收藏

51CTO技术栈公众号