简体   繁体   中英

How to import jsoncpp in cLion?

I am new to C++ and cLion and I'm using JsonCpp to parse object to text and vice-versa. I am a mac user and I have my.h jsoncpp files stored in /usr/include/jsoncpp and.dylib stored in /usr/lib.

What is the a way to include jsoncpp library into my project? I've tried:

include_directories(/usr/include/jsoncpp)

I can #include <json.h> but there is another error as shown below:

Undefined symbols for architecture x86_64:
"Json::Value::Value(Json::ValueType)", referenced from:
      Account::toJson() in Account.cpp.o
      StorageLoad::readfile(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&) in StorageLoad.cpp.o
  "Json::Value::Value(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      Account::toJson() in Account.cpp.o

How to resolve this error? I've spent many hours trying but no avail.

This solution work on CLion 2021.1.3 (Mac Big Sur 11.6)

The solution provided is based on this thread , which discusses a similar problem but with a different library.

Install the library with Homebrew using the Terminal and the command:
brew install jsoncpp

Then, you need the installation location . Either through the output of the installation or using the command:
brew info jsoncpp

The path should be listed and look like this:
/usr/local/Cellar/jsoncpp/1.9.4_1

Then, add the following to your CMakeLists.txt situated in your CLion project (don't forget to use the path you obtained instead).

INCLUDE_DIRECTORIES(  /usr/local/Cellar/jsoncpp/1.9.4_1/include )
LINK_DIRECTORIES(  /usr/local/Cellar/jsoncpp/1.9.4_1/lib )

file(GLOB LIBRARIES "/usr/local/Cellar/jsoncpp/1.9.4_1/lib/*.dylib")
message("LIBRARIES = ${LIBRARIES}")

TARGET_LINK_LIBRARIES(myProjectName ${LIBRARIES})

Reload your CMake (if not done automatically).

Then you are free to use the JSON library you need in your project files using the syntax:

#include <json/ ... >

Example:

#include <json/json.h>
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.h>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM