相关文章推荐
月球上的啄木鸟  ·  Linux下安装Anaconda ...·  10 月前    · 
耍酷的鸡蛋面  ·  Python:numpy ...·  1 年前    · 
另类的勺子  ·  mysql ...·  2 年前    · 
气宇轩昂的蜡烛  ·  类属性: ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Is your include statement correct? The GitHub example shows : #include "json/json.h" .. github.com/open-source-parsers/jsoncpp/blob/master/example/… – FreudianSlip Mar 9, 2022 at 9: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 9, 2022 at 9:22 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 9, 2022 at 9:27 Hi Tom, glad it worked! I guess that a nice thing to do would be to upvote the "solution" comment... – Aname Mar 9, 2022 at 11:02

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.