jansson
jansson copied to clipboard
Target settings do not automatically populate when using CMake FetchContent
MacOS using XCode
Create FetchContent import as follows:
include( FetchContent )
FetchContent_Declare(
jansson
GIT_REPOSITORY https://github.com/akheron/jansson.git
GIT_TAG 684e18c927e89615c2d501737e90018f4930d6c5 # v2.14
)
FetchContent_MakeAvailable(
jansson
)
What works:
- automatic download of Jansson repository
- automatic build of Jansson repository
- inclusion of test targets into generated XCode project
What fails:
- search path to
"jansson.h"not included in build (<jansson.h>also fails) - Jansson compile options not added to build
I enquired on the CMake forums and the FetchContent experts explained that the problem was the Jansson project not using per-target options. Because these are missing, the options are not exported along with the target and the build fails.
Proposed fixes:
-
add_definitions(-DJANSSON_USING_CMAKE)andadd_definitions(-DHAVE_CONFIG_H)becometarget_compile_definitions( jansson PUBLIC JANSSON_USING_CMAKE)andtarget_compile_definitions( jansson PRIVATE HAVE_CONFIG_H)respectively. -
include_directories( ${CMAKE_CURRENT_BINARY_DIR}/include )becomestarget_include_directories(jansson PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include> INTERFACE $<INSTALL_INTERFACE:include> ) - Add library alias
Jansson::jansson - Update minimum CMake to 3.5. As of 3.27 minimums below 3.5 are deprecated.
Testing on OS X with CMake 3.26
- Jansson download and build works
- App build using FetchContent now works correctly
- Can define
Jansson::janssonas dependency -
"jansson.h"header correctly found -
JANSSON_USING_CMAKEcorrectly added to compile options
- Can define
I am interested in working on implementing the proposed solution. Edit: I see this is already being done.
Merged as https://github.com/akheron/jansson/commit/96d160df90016066d04d493d1d69639474ba4f20.