2

I'm testing the library libcbor-0.5.0 for parsing and generating CBOR, https://libcbor.readthedocs.io/en/v0.5.0/index.html. All the examples run correctly, except cjson2cbor.c. It requires cJSON.h, so I added https://github.com/DaveGamble/cJSON to my project.

source code:

void cjson_cbor_stream_decode(cJSON * source,
                              const struct cbor_callbacks *callbacks,
                              void * context)
{
    switch (source->type) {

[...]

case cJSON_Array:
        {
            callbacks->array_start(context, cJSON_GetArraySize(source));
            cJSON *item = source->child;
            while (item != NULL) {
                cjson_cbor_stream_decode(item, callbacks, context);
                item = item->next;
            }
            return;
        }
        case cJSON_Object:
        {
            callbacks->map_start(context, cJSON_GetArraySize(source));
            cJSON *item = source->child;
            while (item != NULL) {
                callbacks->string(context, (unsigned char *) item->string, strlen(item->string));
                cjson_cbor_stream_decode(item, callbacks, context);
                item = item->next;
            }
            return;
        }
[...]

}

...

JSON * json = cJSON_Parse(json_buffer);
cJSON_Delete(json);

If I compile cc cjson2cbor.c -lcbor -o cjson2cbor

it returns this error:

Undefined symbols for architecture x86_64:
  "_cJSON_Delete", referenced from:
      _main in cjson2cbor-d7a32c.o
  "_cJSON_GetArraySize", referenced from:
      _cjson_cbor_stream_decode in cjson2cbor-d7a32c.o
  "_cJSON_Parse", referenced from:
      _main in cjson2cbor-d7a32c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • It's unclear what exactly you did to add cJSON to your project, but it was incomplete. Programs that use cJSON will need to link in that library as well (maybe by passing `-lcjson` to the link command). It may be that you'll need to *build* that library first. If it's not installed in the default library search path, then you'll also need to use an `-L` option to tell the linker where to find it. – John Bollinger Jun 21 '17 at 15:49
  • thank you! the library was imported and built correctly. I try to compile with cc cjson2cbor.c -lcbor -lcjson -o cjson2cbor and it works! :) – Greta Romagnoli Jun 21 '17 at 16:57

1 Answers1

0

Adding the answer @John-Bollinger provided as a community wiki, so this doesn't show up unanswered.

Pass -lcjson in to the link command.

Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48