Search code examples
c++jsonjsoncpp

c++: undefined reference to `Json::Value::Value(Json::ValueType)'


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.


Solution

  • 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