1

I installed jsoncpp on ubuntu with 'sudo apt-get install libjsoncpp-dev' and tried to run a minimalistic test program:

#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>

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

    reader.parse(file,value);
    cout << value << endl;
}

I'm using a makefile to compile my code with the following flags:

CC=g++
CXXFLAGS=-std=c++17 -fopenmp -O3 -ljsoncpp

The following errors occur:

/usr/bin/ld: /tmp/cc4MbV6f.o: in function `Test_JsonWriter()':
test.cpp:(.text+0x5865): undefined reference to `Json::Value::Value(Json::ValueType)'
/usr/bin/ld: test.cpp:(.text+0x5872): undefined reference to `Json::Reader::Reader()'
/usr/bin/ld: test.cpp:(.text+0x5885): undefined reference to `Json::Reader::parse(std::istream&, Json::Value&, bool)'
/usr/bin/ld: test.cpp:(.text+0x5894): undefined reference to `Json::operator<<(std::ostream&, Json::Value const&)'
/usr/bin/ld: test.cpp:(.text+0x5a9f): undefined reference to `Json::Value::~Value()'
/usr/bin/ld: /tmp/cc4MbV6f.o: in function `Test_JsonWriter() [clone .cold]':
test.cpp:(.text.unlikely+0x6bf): undefined reference to `Json::Value::~Value()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: test] Error 1

I also made sure to update all packages etc. Also including '#include <jsoncpp/json/value.h>' doesn't help either.

Any idea what is wrong? Thanks for the help.

Tom
  • 31
  • 1
  • 5
  • Does this answer your question? [Undefined reference Json::Value and Json::Reader](https://stackoverflow.com/questions/39035698/undefined-reference-jsonvalue-and-jsonreader) – InUser Mar 09 '22 at 09:17
  • Is your include statement correct? The GitHub example shows : #include "json/json.h" .. https://github.com/open-source-parsers/jsoncpp/blob/master/example/readFromString/readFromString.cpp – FreudianSlip Mar 09 '22 at 09:19
  • You probably included another copy of JsonCpp in your project (aside from the system's one) and now you are using the headers of one version while trying to link to another version. Either use only the system's version and remove everything else or use only the included copy, which you then have to build and link together with the rest of your project instead of linking with `-ljsoncpp`. – user17732522 Mar 09 '22 at 09:22
  • 3
    Alternatively you have placed the flags incorrectly. `-ljsoncpp` must be placed _after_ the names of the `.cpp` files or `.o` files on the compiler invocation (which you aren't showing) and ususally doesn't belong in `CXXFLAGS`, but a `LDLIBS` variable. – user17732522 Mar 09 '22 at 09:27
  • Thanks a lot, your last comment resolved my issue! – Tom Mar 09 '22 at 09:57

1 Answers1

2

The comment by user17732522 has solved my issue! The flags are placed incorrectly:

"-ljsoncpp must be placed after the names of the .cpp files or .o files on the compiler invocation" -user17732522

Tom
  • 31
  • 1
  • 5
  • Hi Tom, glad it worked! I guess that a nice thing to do would be to upvote the "solution" comment... – Aname Mar 09 '22 at 11:02
  • I'd love to, but can't because I need 15 reputation in order to upvote comments ^^... – Tom Mar 09 '22 at 14:25