0

I'm making program to read and write json file. My code:

#include <bits/stdc++.h>
#include <json/json.h>
#include <json/reader.h>
using namespace std;

int main()
{
    ifstream file("input.json");
    Json::Value value;
    Json::Reader reader;

    reader.parse(file, value);

    cout << value << endl;
    return 0;
}

When I run the program, it's return errors:

C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x3f): undefined reference to `Json::Value::Value(Json::ValueType)'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x4b): undefined reference to `Json::Reader::Reader()'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x6b): undefined reference to `Json::Reader::parse(std::istream&, Json::Value&, bool)'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x7e): undefined reference to `Json::operator<<(std::ostream&, Json::Value const&)'   
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0xaa): undefined reference to `Json::Value::~Value()'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0xdd): undefined reference to `Json::Value::~Value()'

I already try to add param -ljsoncpp but it said C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -ljsoncpp It's there any way to fix this problem? I installed jsoncpp from https://github.com/open-source-parsers/jsoncpp.

  • How did you build jsoncpp? How did you install it? ***Where*** did you install it? – Some programmer dude Aug 03 '22 at 10:49
  • i download from the link i put the end of the post, then t drag and drop include folder to mingw folder. – KaizOffical Aug 03 '22 at 10:51
  • You need to actually configure and build the library. Then install it. Then if it's installed in a non-standard location you need to tell the compiler and linker where to find the library and its header files. – Some programmer dude Aug 03 '22 at 10:56
  • can you give me some example or any tutorial please? @Someprogrammerdude – KaizOffical Aug 03 '22 at 11:00
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Aug 03 '22 at 18:14

1 Answers1

0

I fixed this problem. Just need to run file amalgamate.py (Python >= 2.6 required), it will create three files:

dist/jsoncpp.cpp, the source file to be added to your project
dist/json/json.h, the correspondent header file
dist/json/json-forwards.h, which contains forward declarations of JSON types.

Import file jsoncpp.cpp to your program and it works fine.