1

我想在我的程序中使用 json-c。编译(链接)时出现错误:

parsejson.c:(.text.startup+0xf): undefined reference to `json_object_new_object'
parsejson.c:(.text.startup+0x1c): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x2b): undefined reference to `json_object_new_int'
parsejson.c:(.text.startup+0x3a): undefined reference to `json_object_new_boolean'
parsejson.c:(.text.startup+0x4a): undefined reference to `json_object_new_double'
parsejson.c:(.text.startup+0x52): undefined reference to `json_object_new_array'
parsejson.c:(.text.startup+0x5f): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x6e): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x7b): undefined reference to `json_object_new_string'
parsejson.c:(.text.startup+0x8b): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0x96): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0xa1): undefined reference to `json_object_array_add'
parsejson.c:(.text.startup+0xb3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xc3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xd3): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xe5): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xf5): undefined reference to `json_object_object_add'
parsejson.c:(.text.startup+0xfd): undefined reference to `json_object_to_json_string'

我将 json-c 和我的程序放在同一个文件夹中,并使用#include <json-c/json.h>.

4

3 回答 3

1

静态链接时,gcc 会带上已经遇到的符号。因此,如果您-ljson在源文件之前传递,gcc 将采用静态库,然后最终不需要任何东西。因此,您应该在代码之后放置要链接的库。

尽管您尚未分享编译命令行的内容,但我建议您尝试以下操作:

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/compiled/library parsejson.c -o parsejson -ljson
于 2015-06-25T05:16:54.580 回答
1

尝试使用这个:

#include "../json-c/json.h"

因为如果您使用编译器将在标准库中搜索 json.h。显然,它不在标准库中。如果您使用我告诉您的内容,编译器将在当前工作区中搜索 json.h。

于 2016-03-11T04:52:24.640 回答
0

尝试使用

gcc parsejson.c -o parsejson -ljson-c

编译和使用

#include "json-c/json.h"

包括标题

于 2021-08-11T13:46:16.740 回答