Undefined reference Json::Value and Json::Reader

user9492428 picture user9492428 · Aug 19, 2016 · Viewed 8.8k times · Source

When I run the following code:

#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;
}

I get the following errors:

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

I am trying to write a program to read the JSON file and this code also has to output the data in the JSON file to be used by another C++ module.

UPDATE

#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;
}

I have the changed the code to remove the full link and inserted the dist folder I got after I ran:

python amalgamate.py

And I entered the header files into C:\MinGW\include

I am now getting a lot of errors in the jsoncpp.cpp file (this is the file I got after running the python command and I did not change it at all). All the errors say the same message, which is:

first defined here

Answer

tty6 picture tty6 · Aug 19, 2016

It's not enough to just include h files to your source code. You need to compile jsoncpp.cpp in your project. Please follow https://github.com/open-source-parsers/jsoncpp#generating-amalgamated-source-and-header and add jsoncpp.cpp, json/json.h, json/forwards.h to your project.