【问题标题】:Undefined reference error while using json-c使用 json-c 时出现未定义的引用错误
【发布时间】:2015-06-25 04:34:38
【问题描述】:

我想在我的程序中使用 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> 包含它。

【问题讨论】:

  • 显示你的编译命令行。您是否在parsejson.cparsejson.o 之后列出库,无论您在链接命令行中列出哪个?你建好 json-c 库了吗?你在链接它吗?
  • 不,我只是在使用命令“gcc parsejson.c”,json-c 是第三方库,但我使用#include 包含
  • 标头提供声明。它通常不提供实现。您需要与需要编译和安装的库链接(取决于它是共享的还是静态的)。图书馆的自述文件没有告诉你这个吗?

标签: c json json-c


【解决方案1】:

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

虽然您没有分享编译命令行的内容,但我建议您尝试以下操作:

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/compiled/library parsejson.c -o parsejson -ljson

【讨论】:

  • 您不会将-Lparsejson.c 的整个路径一起使用;您可以使用parsejson.c 的完整路径,或者您可以使用-L 与包含已编译json-c 库的目录。或者您在命令行中错过了元字符 > 和名称 parsejson.c 之间的空格。
  • 它不起作用说 bash: -L: 没有这样的文件或目录
  • @Jonathan:是的,我错过了>parsejson.c 之间的空格。我现在将对其进行编辑。谢谢。
  • @Sparsh:我希望你没有使用<>。 (你应该忽略它)
  • @WedaPashi 是的,我省略了 但仍然出现错误
【解决方案2】:

尝试使用这个:

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

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

【讨论】:

    【解决方案3】:

    尝试使用

    gcc parsejson.c -o parsejson -ljson-c

    编译使用

    #include "json-c/json.h"

    包含标题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-15
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2015-03-28
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多