how to write a cmake Module for jsoncpp? how to write a cmake Module for jsoncpp? json json

how to write a cmake Module for jsoncpp?


Ok, I have a solution that compiles fine on my system. Finding jsoncpp is tricky, because json-c installs a header with the same name, and on my system, that header is located under /usr/include/json/json.h. To get it work, you have to make the following changes:


in FindJsoncpp.cmake:

# Include dirfind_path(Jsoncpp_INCLUDE_DIR  NAMES json/features.h  PATH_SUFFIXES jsoncpp  PATHS ${Jsoncpp_PKGCONF_INCLUDE_DIRS} # /usr/include/jsoncpp/json)

Searching for json/features.h instead of json/json.h avoids finding the json.h file of json-c on my system, which is not compatible.


in CMakeLists.txt :

include_directories(${Jsoncpp_INCLUDE_DIR})add_executable(jsonparser jsonparser.cpp)target_link_libraries(jsonparser ${Jsoncpp_LIBRARY})

Here the found directories are set up, so CMake actually uses them.


in jsonparser.cpp:

const Json::Value songs = root["songs"];for ( int index = 0; index < songs.size(); ++index ){  // Iterates over the sequence elements.   std::clog<<"Name="<<songs[index]["name"];   std::clog<<"Artist="<<songs[index]["artist"];}

Your orginal code didn't compile, so I replaced the offending piece with the code above. Have you forgot to declare the song variable?


I also removed the getFormattedErrorMessages() call, because I have only jsoncpp 0.5.0, in which that function is not available. That shouldn't make a difference though.

Let me know if this works for you.


jsoncpp now builds with cmake.

cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -G "Unix Makefiles" ../..
  • supports pkg-config
  • builds static lib, dynamic lib, or both
  • can be included into other projects

If you have suggestions, open an issue at GitHub.