2

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.

stackyyflow
  • 767
  • 3
  • 11
  • 30
  • 1
    You will need to link the library, `target_link_libraries(your_executable jsoncpp)`, (if that is the name of the .dylib) – melak47 Dec 13 '15 at 03:00
  • Do I need to specify the location of jsoncpp.dylib when I link the library? Since mine is in /usr/lib. Sorry I am still new :) Or the lib should be in my project instead? – stackyyflow Dec 13 '15 at 03:11
  • 1
    `usr/lib` should be a default path the compiler/linker checks, but if not, add it with the [link_directories](https://cmake.org/cmake/help/v3.3/command/link_directories.html) command. – melak47 Dec 13 '15 at 04:29
  • ok it works now thank you :) @melak47 – stackyyflow Dec 13 '15 at 08:15
  • It's working by accident because jsoncpp is installed to a default system-wide location on your system. You should be using a cmake find package module (such as [described here](http://stackoverflow.com/questions/18005880/how-to-writing-a-cmake-module-for-jsoncpp)) to locate jsoncpp and then add the appropriate include and link settings. – legalize Dec 15 '15 at 22:21

1 Answers1

0

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>
...
Vincent B
  • 41
  • 3