1

Openharmony L1级设备之CJSON库调用-51CTO.COM

 2 years ago
source link: https://os.51cto.com/article/701832.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Openharmony L1级设备之CJSON库调用-51CTO.COM
Openharmony L1级设备之CJSON库调用 原创
作者:古城青松 2022-02-17 17:52:00
本文将详细讲解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$ 

上传至开发板:

b944d3337f449950d1a82921b69aa8c290fc16.png

运行结果:

b82bbce9020d812fc40733d4445d1335124d78.png

开发注意事项

[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”
]

编译依旧报错。

992c8054786cabdba23290284850469d7a3b2d.png

继而采用使用public_deps 依赖,才避免编译报错。

 public_deps = [
     "//build/lite/config/component/cJSON:cjson_shared",
  ]

使用系统自带的三方库时,可以参考以上开发踩得坑,尽量使用public_deps,来避免依赖问题。当然自己开发的动态库,非系统的可以

参考https://harmonyos.51cto.com/posts/10353.

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

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

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

18c38d2147416e15f4a48865f20ac1dcf0eb43.jpg

责任编辑:jianghua 来源: 鸿蒙社区
zanpc.bd208a1.pngzanpchover.fdd60ba.png
weixin.23cd8b3.png 分享到微信
weibo.16d6b4f.png 分享到微博

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK