Search code examples
c++cmakejsoncpp

How to use jsoncpp from fetchcontent


I'm making one program that is basically a stresstest for a couple of different machines, and I have to write the testresults to a jsonformat.

Because I don't want to manually install jsoncpp on every machine I decided to use Fetchcontent in the CMakeLists file:

cmake_minimum_required(VERSION 3.15)
project(Programma)

include(FetchContent)

FetchContent_Declare(
        jsoncpp
        GIT_REPOSITORY https://github.com/open-source-parsers/jsoncpp.git
        GIT_TAG        master
)

FetchContent_GetProperties(jsoncpp)
if (NOT jsoncpp_POPULATED)
    FetchContent_Populate(jsoncpp)
    add_subdirectory(${jsoncpp_SOURCE_DIR} ${jsoncpp_BINARY_DIR})
    message(${jsoncpp_SOURCE_DIR})
    message(${jsoncpp_BINARY_DIR})
endif ()

#FetchContent_MakeAvailable(jsoncpp)

set(CMAKE_CXX_STANDARD 17)

add_executable(Programma main.cpp)
add_library(TestSubjects.cpp TransformTests.cpp FoldTests.cpp
        TestResults.h SortTests.cpp FindTests.cpp)

target_link_libraries(Programma Tests jsoncpp)

but I tried a couple of header includes like <json.h> <jsoncpp/json.h> json/json.h> but none of them work. What am I doing wrong?


Solution

  • While being built, the project jsoncpp doesn't provide jsoncpp target. Instead, it provides separate targets for different kind of library:

    • jsoncpp_static for STATIC library,
    • jsoncpp_lib for SHARED library,
    • jsoncpp_object for OBJECT library.

    By default, all 3 library kinds are built, so you may choose any of them for link with:

    target_link_libraries(Programma jsoncpp_lib)
    

    Also, the correct include directive is

    #include <json/json.h>