Problems including jsonCpp headers

19,905

Solution 1

Your code is compiling, but it is not linking. You forgot to provide the JSON shared library files to your linker (or, on newer versions, to add the amalgamated jsoncpp.cpp to your project).

Without knowing more about your development environment, it's hard to give you more specific instructions.

BTW, you're writing C++; use C++ headers like cstdio, not stdio.h, please. You also failed to include C++ string and got lucky that it "worked" through some JSON header including it for you.

Solution 2

"Undefined reference" sounds like a linker problem. Does jsoncpp come with a library that you need to link to, such as a .so, .a, .lib or .dll file?

According to the jsoncpp README, the library must first be built using scons. Presumably this will then output a library file such as a .so, .a, .lib or .dll file. You must then follow your compiler's rule for linking against such a library (e.g. add it to the end of the command line when compiling, or add it to the "additional libraries" field in the project config in your IDE).

Solution 3

In my case (using CodeBlocks IDE on Ubuntu) the problem was that I needed to add the json.cpp file (generated with python amalgamate.py from within the jsoncpp project) to my build targets.

In other words, I added a -c jsoncpp.cpp option to my g++ compile statement.

Share:
19,905
Jayden Le
Author by

Jayden Le

Updated on June 04, 2022

Comments

  • Jayden Le
    Jayden Le over 1 year

    I'm trying to implement the jsoncpp libraries in my C++ code, I wrote a simple piece of code just to try it out, and it's not even compiling.

    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    
    #ifndef json_included
    #define json_included
    #include "jsoncpp\include\json\json.h"
    #endif
    
    //#include "json\jsonC\json.h"
    int main(int argc, char **argv)
    {
    
    std::string example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
    Json::Value value;
    Json::Reader reader;
    
    bool parsed = reader.parse(example, value, false);
    std::cout << parsed;
    return 0;
    }
    

    The errors i'm getting are:

    undefined reference to `Json::Reader::parse(std::string const&, Json::Value&, bool)'
    undefined reference to `Json::Reader::Reader()'
    undefined reference to `Json::Value::~Value()'
    undefined reference to `Json::Value::Value(Json::ValueType)'
    

    I'm a bit new to C++, is there something I'm missing in the include statement? Or does jsonCpp need something extra?

    Thank you for your time!