编译和打印文件时发生cJSON错误

编译和打印文件时发生cJSON错误,c,gcc,installation,cjson,C,Gcc,Installation,Cjson,我最近通过Cmake安装了David Gamble的cJSON库,我收到以下错误: gcc prueba.c -lm -o prueba /tmp/ccdmegU5.o: In function `main': prueba.c:(.text+0x2e): undefined reference to `cJSON_Parse' prueba.c:(.text+0x45): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+

我最近通过
Cmake
安装了David Gamble的cJSON库,我收到以下错误:

gcc prueba.c -lm -o prueba /tmp/ccdmegU5.o: In function `main': prueba.c:(.text+0x2e): undefined reference to `cJSON_Parse' prueba.c:(.text+0x45): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+0x60): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+0xa2): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+0xb2): undefined reference to `cJSON_GetArraySize' prueba.c:(.text+0xdb): undefined reference to `cJSON_GetArrayItem' prueba.c:(.text+0xf2): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+0x10d): undefined reference to `cJSON_GetObjectItem' prueba.c:(.text+0x146): undefined reference to `cJSON_Delete' collect2: error: ld returned 1 exit status gcc prueba.c-lm-o prueba /tmp/ccdmegU5.o:在函数“main”中: prueba.c:(.text+0x2e):对“cJSON\u Parse”的未定义引用 prueba.c:(.text+0x45):对“cJSON_GetObjectItem”的未定义引用 prueba.c:(.text+0x60):对“cJSON_GetObjectItem”的未定义引用 prueba.c:(.text+0xa2):对“cJSON_GetObjectItem”的未定义引用 prueba.c:(.text+0xb2):对“cJSON_GetArraySize”的未定义引用 prueba.c:(.text+0xdb):对“cJSON_GetArrayItem”的未定义引用 prueba.c:(.text+0xf2):对“cJSON_GetObjectItem”的未定义引用 prueba.c:(.text+0x10d):对“cJSON_GetObjectItem”的未定义引用 prueba.c:(.text+0x146):对“cJSON\u Delete”的未定义引用 collect2:错误:ld返回了1个退出状态 在尝试编译以下简单的.c代码时:

int main(int argc, const char * argv[]) {

/*{
    "name": "Mars",
    "mass": 639e21,
    "moons": [
        {
            "name": "Phobos",
            "size": 70
        },
        {
            "name": "Deimos",
            "size": 39
        }
    ]
}*/
char *strJson = "{\"name\" : \"Mars\",\"mass\":639e21,\"moons\":[{\"name\":\"Phobos\",\"size\":70},{\"name\":\"Deimos\",\"size\":39}]}";

printf("Planet:\n");
// First, parse the whole thing
cJSON *root = cJSON_Parse(strJson);
// Let's get some values
char *name = cJSON_GetObjectItem(root, "name")->valuestring;
double mass = cJSON_GetObjectItem(root, "mass")->valuedouble;
printf("%s, %.2e kgs\n", name, mass); // Note the format! %.2e will print a number with scientific notation and 2 decimals
// Now let's iterate through the moons array
cJSON *moons = cJSON_GetObjectItem(root, "moons");
// Get the count
int moons_count = cJSON_GetArraySize(moons);
int i;
for (i = 0; i < moons_count; i++) {
    printf("Moon:\n");
    // Get the JSON element and then get the values as before
    cJSON *moon = cJSON_GetArrayItem(moons, i);
    char *name = cJSON_GetObjectItem(moon, "name")->valuestring;
    int size = cJSON_GetObjectItem(moon, "size")->valueint;
    printf("%s, %d kms\n", name, size);
}

// Finally remember to free the memory!
cJSON_Delete(root);
return 0;
}
int main(int argc,const char*argv[]{
/*{
“名称”:“火星”,
“质量”:639e21,
“月亮”:[
{
“名称”:“火卫一”,
“尺寸”:70
},
{
“名称”:“Deimos”,
“尺寸”:39
}
]
}*/
char*strJson=“{\'name\':\'Mars\',\'mass\':639e21,\'moons\':[{\'name\':\'Phobos\',\'size\':70},{\'name\':\'Deimos\',\'size\':39}”;
printf(“行星:\n”);
//首先,分析整个过程
cJSON*root=cJSON_Parse(strJson);
//让我们得到一些值
char*name=cJSON_GetObjectItem(根,“名称”)->valuestring;
双质量=cJSON_GetObjectItem(根,“质量”)->valuedouble;
printf(“%s,%.2e kgs\n”,名称,质量);//注意格式!%.2e将打印带有科学符号和2位小数的数字
//现在让我们遍历moons数组
cJSON*moons=cJSON_GetObjectItem(根,“moons”);
//数一数
int moons_count=cJSON_GetArraySize(moons);
int i;
对于(i=0;ivaluestring;
int size=cJSON_GetObjectItem(月亮,“大小”)->valueint;
printf(“%s,%d kms\n”,名称,大小);
}
//最后记得释放记忆!
cJSON_删除(根目录);
返回0;
}

如果我将cJSON.c的内容添加到我的代码中,它会解决问题,但会打印一个损坏的文件。

您需要使用
-lcjson
编译它,以便将您的代码与已安装的cJSON库相链接