我正在测试库libcbor-0.5.0以解析和生成CBOR,https://libcbor.readthedocs.io/en/v0.5.0/index.html。除cjson2cbor.c外,所有示例都正确运行。 它需要cJSON.h,所以我将https://github.com/DaveGamble/cJSON添加到我的项目中。
源代码:
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);
如果我编译 cc cjson2cbor.c -lcbor -o cjson2cbor
它会返回此错误:
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)
答案 0 :(得分:0)
添加作为社区Wiki提供的答案@ John-Bollinger,因此不会出现未回答的情况。
将-lcjson
传递给链接命令。