Unable to resolve this cjson library issue

Hi Team,

I’m getting this error while compile and build the code which has cjson,
I’m new to c code please help me with step by step, please find the response below

opt/swi/SWI9X07Y_02.37.07.00/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lcjson

you can try this:

  1. download the yocto source from source.sierrawireless.com

https://source.sierrawireless.com/resources/airprime/software/wp76xx/wp76xx-firmware-release-16,-d-,0,-d-,1-components/#sthash.ch7Rl9Ia.dpbs

  1. go to ./meta-swi/meta-swi-mdm9x28/recipes-core/images/mdm9x28-image.inc

3.add the following line:
IMAGE_INSTALL_append = " json-c"

  1. Make the yocto image

  2. the include path now should be in yocto/build_bin/tmp/work/armv7a-neon-poky-linux-gnueabi/json-c/0.13-r0/image/usr/include/json-c/
    you need to add the following in cflag:

     -Iyocto/build_bin/tmp/work/armv7a-neon-poky-linux-gnueabi/json-c/0.13-r0/image/usr/include/json-c/
    
  3. for the linker (-l parameter), you should point to this path
    yocto/build_bin/tmp/work/armv7a-neon-poky-linux-gnueabi/json-c/0.13-r0/image/usr/lib/libjson-c.so.3.0.1

Here is a zip file for the compiled binary compiled by WP76 FW R16.0.01:
image.zip (269.0 KB)

Hi @jyijyi ,

Thank you for the support, the library which i’m looking is GitHub - DaveGamble/cJSON: Ultralightweight JSON parser in ANSI C this one, can you generate for this, due to lack of knowledge I’m failing to build as per you instruction

The instruction says you can just copy cJSON.h and cJSON.c to your projects source

Do you find problem to compile it?

I have just tried this:
cjson.zip (37.9 KB)

I can compile by this:
owner@ubuntu:~/Downloads/cjson$ /opt/swi/y22-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -o cjson.o cJSON.c test.c

Hi @jyijyi ,

I have landed in new issue, the gateway which I’m having is preloaded with readonly root file system no option to upload json-c new version of shared library now I need I need to generate libjson-c.so.3 and libjson-c.so.3.0.1 for x86, I have ARM complatible but getting the error below , please help

/opt/swi/SWI9X07Y_02.37.07.00/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -ljson-c
collect2: error: ld returned 1 exit status

do you mean you want to put libjson-c.so.3 and libjson-c.so.3.0.1 in module’s /usr/lib folder?

If yes, you can first enter the following two commands to do overlay:


mkdir /tmp/tmp_usr_lib;mkdir /tmp/tmp_usr_lib_wr;

mount -t overlay overlay /usr/lib -o lowerdir=/usr/lib,upperdir=/tmp/tmp_usr_lib,workdir=/tmp/tmp_usr_lib_wr;

Hi @jyijyi
I manged to install libjson-c.a libjson-c.so libjson-c.so.3 libjson-c.so.3.0.1 and getting output but same libraries arm built when I tried to cross compile this is the error I’m getting error, below

/opt/swi/SWI9X07Y_02.37.07.00/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -ljson-c
collect2: error: ld returned 1 exit status

I have use -L/path also same issue

What could be the reason

can you post your sample code here?
How did you compile?
can you post the command you used to compile?

BTW, have you tried to put the .so library in the same folder of the source code?

Hi @jyijyi

sample code below


#include <mosquitto.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <json-c/json.h>
#include <modbus.h>
#include <errno.h>
#include <modbus.h>
#include <unistd.h>

void on_connect(struct mosquitto *mosq, void *obj, int reason_code)
{

printf("on_connect: %s\n", mosquitto_connack_string(reason_code));
if(reason_code != 0){

	mosquitto_disconnect(mosq);
}

}

void on_publish(struct mosquitto *mosq, void *obj, int mid)
{
printf(“Message with mid %d has been published.\n”, mid);
}

void publish_sensor_data(struct mosquitto *mosq)
{
modbus_t *ctx;
uint16_t data[113]; // buffer to hold the data read
int rc, i;
uint32_t sec_to = 10;
uint32_t usec_to = 0;
char key[114];
char value[114];

int nb_points = 1;
const int slave_address = 10; // the slave address of the Modbus device

// create a new context for the Modbus communication
ctx = modbus_new_rtu(“/dev/ttyHS1”, 9600, ‘E’, 8, 1);
modbus_get_response_timeout(ctx, &sec_to, &usec_to);
if (ctx == NULL) {
fprintf(stderr, “Error creating Modbus context\n”);
//return 1;
}

// set the slave address of the device we want to communicate with
modbus_set_slave(ctx, slave_address);

// connect to the Modbus device
if (modbus_connect(ctx) == -1) {
fprintf(stderr, “Error connecting to Modbus device: %s\n”, modbus_strerror(errno));
modbus_free(ctx);

//return 1;
}

// read 10 holding registers starting at address 0
rc = modbus_read_input_registers(ctx, 0, 113, data);
if (rc == -1) {
fprintf(stderr, “Error reading Modbus data: %s\n”, modbus_strerror(errno));
modbus_close(ctx);
modbus_free(ctx);
//return 1;
}
json_object *my_object = json_object_new_object();

if (my_object == NULL) {
    printf("Error creating JSON object!\n");
    //return EXIT_FAILURE;
}

// print the data that was read
for (i = 0; i < 114; i++) {
printf(“Data[%d]:%X\n”, i+1, (data[i]));
sprintf(key, “Reg_%d”, i+1);
sprintf(value, “%x”, (data[i]));
// cJSON_AddItemToObject(json, key, cJSON_CreateString(value));
json_object_object_add(my_object, key, json_object_new_string(value));
}
const char *json_string = json_object_to_json_string(my_object);
printf(“JSON: %s\n”, json_string);

// disconnect from the Modbus device and clean up

rc = mosquitto_publish(mosq, NULL, "newtopic", strlen(json_string), json_string, 0, false);
if(rc != MOSQ_ERR_SUCCESS){
	fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(rc));
}

modbus_close(ctx);
modbus_free(ctx);
sleep(3);
}

int main()
{
struct mosquitto *mosq;
int rc;

mosquitto_lib_init();


mosq = mosquitto_new("TestClient", true, NULL);
if(mosq == NULL){
	fprintf(stderr, "Error: Out of memory.\n");
	return 1;
}

mosquitto_tls_set(mosq, "./AmazonRootCA1.pem", NULL, "./certificate.pem.crt", "./private.pem.key", NULL);
/* Configure callbacks. This should be done before connecting ideally. */
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_publish_callback_set(mosq, on_publish);


rc = mosquitto_connect_async(mosq, "aws-endpoint", 8883, 60);
if(rc != MOSQ_ERR_SUCCESS){
	mosquitto_destroy(mosq);
	fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
	return 1;
}


rc = mosquitto_loop_start(mosq);
if(rc != MOSQ_ERR_SUCCESS){
	mosquitto_destroy(mosq);
	fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
	return 1;
}


while(1){
	publish_sensor_data(mosq);
}

mosquitto_lib_cleanup();
return 0;

}


cross compile commands below

  1. source source /opt/swi/SWI9X07Y_02.37.07.00/environment-setup-armv7a-neon-poky-linux-gnueabi

  2. $CC modbus_mqtt8.c -o out1.o -lmodbus -lmosquitto -ljson-c

please note inside gateway libjson-c.so.3 libjson-c.so.3.0.1 these are shared libraries available

So I installed same x86_64 version on my system also and gcc compile is successful (modbus_mqtt8.c -o out1.o -lmodbus -lmosquitto -ljson-c) i’m getting executable successfully

please note: aws-end point is there which i removed

I don’t see problem with this:

cjson_link_lib.zip (112.9 KB)

owner@ubuntu:~/Downloads/cjson/cjson_link_lib$ sudo cp ./libjson-c.so  /opt/swi/y22-ext/sysroots/armv7a-neon-poky-linux-gnueabi/usr/lib/
owner@ubuntu:~/Downloads/cjson/cjson_link_lib$ sudo /opt/swi/y22-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -ljson-c -o cjson.o  test2.c 
owner@ubuntu:~/Downloads/cjson/cjson_link_lib$ ls
a.out  cjson.o  json-c  libjson-c.so  test2.c

Hi @jyijyi

Thanks a lot, It work I successfully completed my work and submitted. Thanks you for the support since I’m not from embedded firmware background and not a C programmer though, your suggestions and lib file resolved my issue.

I’m curious to know one thing, I too has libjson-c.so.3 and libjson-c.so.3.0.1 for both x86 and arm they were in same folder though then why it didn’t worked and how you create that shared library ?

you are using the toolchain to cross compile, so you need to use libjson library cross-compiled for the application, and you cannot use the x86 library…

As mentioned above, i built the library in yocto linux source:

BTW, here teaches you how to debug when you see library cannot be found by linker:

e.g.
/opt/swi/y22-ext/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/6.2.0/real-ld -ljson-c --verbose

libjson-c.so.zip (46.9 KB)

Hi @jyijyi

Please find the attachment this shared lib, its ARM build only

I can compile with your library:

owner@ubuntu:~/Downloads/cjson/cjson_link_lib$ sudo cp ./libjson-c_novin.so.3  /opt/swi/y22-ext/sysroots/armv7a-neon-poky-linux-gnueabi/usr/lib/libjson-c_novin.so
owner@ubuntu:~/Downloads/cjson/cjson_link_lib$ sudo /opt/swi/y22-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -ljson-c_novin -o cjson.o  test2.c

Hi @jyijyi ,

Unfortunately it was not working when I did the same, but your library worked :slight_smile: