【问题标题】:Undefined reference Json::Value and Json::Reader [duplicate]未定义的引用 Json::Value 和 Json::Reader [重复]
【发布时间】:2016-08-19 09:33:07
【问题描述】:

当我运行以下代码时:

#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include <C:\Users\User\Documents\jsoncpp-master\dist\json\json.h>
#include <C:\Users\User\Documents\jsoncpp-master\dist\json\json-forwards.h>

using namespace std;

int main(){
    Json::Value root;
    Json::Reader reader;

    ifstream file("test.json");

   return 0;
}

我收到以下错误:

undefined reference to `Json::Reader::Reader()'
undefined reference to `Json::Value::Value(Json::ValueType)'

我正在尝试编写一个程序来读取 JSON 文件,并且此代码还必须输出 JSON 文件中的数据以供另一个 C++ 模块使用。

更新

#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include "dist\jsoncpp.cpp"
using namespace std;

int main(){

    Json::Value root;
    Json::Reader reader;

    ifstream file("test.json");

    return 0;
}

我已更改代码以删除完整链接并插入我运行后得到的 dist 文件夹:

python amalgamate.py

我将头文件输入到 C:\MinGW\include

我现在在 jsoncpp.cpp 文件中遇到很多错误(这是我运行 python 命令后得到的文件,我根本没有更改它)。所有的错误 说出同样的信息,即:

first defined here

【问题讨论】:

  • Json 类在哪里定义?
  • 链接器错误。您必须与 jsoncpp 库链接。
  • [OT]:不要使用包含的完整路径。为您的构建系统指定其他目录以查找标头。
  • 尝试以下解决方案:stackoverflow.com/questions/5174019/…
  • 你是如何构建你的项目的?你是如何链接 JsonCpp 的?你有关于如何使用 JsonCpp 的 RTFM 吗? (好像没有)

标签: c++ json jsoncpp


【解决方案1】:

仅在源代码中包含 h 文件是不够的。 您需要在项目中编译 jsoncpp.cpp。 请关注https://github.com/open-source-parsers/jsoncpp#generating-amalgamated-source-and-header 并将 jsoncpp.cpp、json/json.h、json/forwards.h 添加到您的项目中。

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2016-08-12
    • 2023-03-23
    • 2012-12-04
    相关资源
    最近更新 更多