c++ - 尝试在 C++ 中使用 JSON Spirit 将 JSON 写入文件

标签 c++ json json-spirit

我正在尝试使用 JSON Spirit 将 JSON 写入文件.

我正在使用类似于网站上给出的示例的代码来执行此操作,如下所示:

json_spirit::Object emotion_json;
emotion_obj.push_back(json_spirit::Pair("Dominant emotion", "Joy"));

ofstream os("emotion_json.json");
json_spirit::write(emotion_obj, os);
os.close();

我不断收到错误:

undefined reference to 'json_spirit::write(json_spirit::Value_impl<json_spirit::Config_vector<std::string> > const&, std::ostream&, int, unsigned int)'

我尝试了 JSON Spirit 的不同变体以将 JSON 输出到文件,但尽管有 header ,但它们似乎都不适合我:

#include <json_spirit.h>
#include <json_spirit_writer_template.h>
#include <json_spirit_writer.h>

知道会发生什么吗?另外,如果有更好的轻量级库来用 C++ 创建 JSON 结构,我会洗耳恭听。谢谢!

最佳答案

打开链接选项:

-ljson_spirit

如果编译器找不到json_spirit,使用

-L /path/to/the/parent/folder/of/libjson_spirit.a 

json_spirit之前和.cpp文件名之后。

如果您找不到任何 libjson_spirit.a,您应该构建该库。

转到源代码中的文件夹 json_spirit,您会在其中看到文件 CMakeLists.txt

在命令行输入:

cmake .
make

然后 libjson_spirit.a 将被创建。

关于c++ - 尝试在 C++ 中使用 JSON Spirit 将 JSON 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47321829/

相关文章:

java - 如果网络可用,则从服务器获取 JSON

c - gcc不包含c头文件json-c

c++ - 运算符重载 : getting unidentifiable errors

c++ - 是否存在检测有符号类型的位移位操作的 GCC 警告?

java - 如何使用简单的 json 在请求正文下传递 json 数组

c++ - JSON_Spirit : how to get value

c++ - 如何用json精神读取递归json数据

c++ - 使用 JSON Spirit 进行 pretty-print

c++ - "Abort signal from abort(3) (SIGABRT)"。为什么只针对某些情况?

c++ - 使用大括号将临时值初始化为静态数据成员的初始化程序会导致错误