C 包含库失败

标签 c gcc linker undefined-reference

我是 C 的新手,我想试试 this json de/序列化器。因为它不需要任何依赖,所以我只是将代码放在我项目的子目录中。

/
    json_test.c
    json/
        json.c
        json.h

json_test.c 来源:

#include "json/json.h"
int main() {
    JsonNode *json = json_mkobject();
}

以及 json/json.h 的一部分:

JsonNode *json_mkobject(void);

问题是我无法使用库中的任何函数,因为编译失败(我认为是链接器):

$ gcc json_test.c -o json_test
/tmp/cco4ZDh0.o: in function « main »:
json_test.c:(.text+0x9): undefined reference to « json_mkobject »
collect2: error: ld returned 1 exit status

我从昨天开始就在做,你有什么建议让它发挥作用吗?

最佳答案

你还应该编译 json/json.c:

gcc json/json.c json_test.c -I json -o json_test

关于C 包含库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367235/

相关文章:

Cortex-M4 锁定

c++ - 如何解决链接器错误 "cannot find -lgcc_s"

c++ - 在 C 中管理枚举变量(从 C++ 移植代码)

c++ - 替换字符串中空格的策略

c - 无法确定汇编语言的返回值

android - Android 上的 LD_PRELOADING dlopen

c - Sqlite3 结构未在 Eclipse 中解析

linux - 段错误 : 0x0000000000000001 in ?? () 试图在 Linux 下编译/链接

c - 动态库 (.so) : Link time and run time

c++ - CMake:在静态库上设置链接器标志,供消费者在链接时使用 (Visual Studio)