未定义的引用Json :: Value和Json :: Reader

时间:2016-08-19 09:33:07

标签: c++ json jsoncpp

当我运行以下代码时:

#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

1 个答案:

答案 0 :(得分:1)

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