I’ve been happily using FetchContent
in my new projects to resolve dependencies and its been super slick. Now I need a JSON parser and cJSON has CMake build support so its a win!
However the cJSON CMakeLists.txt
file was written a while ago and does something weird with its headers. Here’s the section in particular:
file(GLOB HEADERS cJSON.h)
# ~snip~
if (NOT BUILD_SHARED_AND_STATIC_LIBS)
add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}")
else()
add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}")
add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}")
set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}")
set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib")
endif()
if (NOT WIN32)
target_link_libraries("${CJSON_LIB}" m)
endif()
So it seems like they’re attaching the headers to the library but not in the same way that I’ve been using. I tried adding the following line which fixed this problem in my own libs:
target_include_directories("${CJSON_LIB}" PUBLIC "${PROJECT_SOURCE_DIR}/")
But that results in a bunch of errors in the configure stage:
CMake Error in build-debug/_deps/cjson-src/CMakeLists.txt:
Target "cjson" INTERFACE_INCLUDE_DIRECTORIES property contains path:
"/home/chrinkus/Repos/advent-of-code/2015/c/build-debug/_deps/cjson-src/"
which is prefixed in the build directory.
CMake Error in build-debug/_deps/cjson-src/CMakeLists.txt:
Target "cjson" INTERFACE_INCLUDE_DIRECTORIES property contains path:
"/home/chrinkus/Repos/advent-of-code/2015/c/build-debug/_deps/cjson-src/"
which is prefixed in the build directory.Target "cjson"
INTERFACE_INCLUDE_DIRECTORIES property contains path:
"/home/chrinkus/Repos/advent-of-code/2015/c/build-debug/_deps/cjson-src/"
which is prefixed in the source directory.
Is there a way for me to connect to cJSON’s headers through a generated variable or should I be using find_package
? Or something else?
Thank you!