cJSON icon indicating copy to clipboard operation
cJSON copied to clipboard

Installing with CMake's FetchContent creates problems with header files

Open eulersson opened this issue 2 years ago • 2 comments

Hello, I am trying to install cJSON with CMake's FetchContent and I managed to do it but I find it strange that the header files are not included in the cjson target.

I am not an expert with CMake so apologies if my question is stupid.

project(myproject)
include(FetchContent)
FetchContent_Declare(
  cJSON
  GIT_REPOSITORY https://github.com/DaveGamble/cJSON
  GIT_TAG        cb8693b058ba302f4829ec6d03f609ac6f848546 # v1.7.16
)
set(ENABLE_CJSON_TEST OFF CACHE BOOL "cJSON: Build with unit testing" FORCE)
FetchContent_MakeAvailable(cJSON)

// The three lines below would be replaced by a single one `target_link_libraries(${PROJECT_NAME} PUBLIC cjson)`
// if the cJSON project would include the header files as part of the target cjson?
configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include)
target_link_libraries(${PROJECT_NAME} PUBLIC cjson)

I am wondering if there's a way that I can compile against cjson without hacking a build-time ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h file. As far as I know it would work if the developers of cJSON would have used target_include_directories on the root CMakeLists.txt?

Thank you!

eulersson avatar Dec 29 '23 11:12 eulersson

try this

cmake_minimum_required(VERSION 3.28...3.30)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(cJSON-test VERSION 0.1.0 LANGUAGES CXX)

add_compile_options(-std=c17)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_EXTENSIONS NO)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_BUILD_TYPE Release)

set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/stagedir)

enable_testing()

include(FetchContent)

FetchContent_Declare(
  cJSON
  GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
  GI_TAG "v1.7.18"
  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cJSON-src"
  GIT_SHALLOW TRUE
)

set(ENABLE_CUSTOM_COMPILER_FLAGS OFF)
set(BUILD_SHARED_AND_STATIC_LIBS ON)

FetchContent_MakeAvailable(cJSON)

set(CPACK_GENERATOR TGZ)
include(cpack)

ClausKlein avatar Jul 17 '24 06:07 ClausKlein

the issue still exist,the gemini descrisption:

Image

yankedi avatar Apr 26 '26 13:04 yankedi