1# CMake 2 3You can also use the `nlohmann_json::nlohmann_json` interface target in CMake. This target populates the appropriate usage requirements for `INTERFACE_INCLUDE_DIRECTORIES` to point to the appropriate include directories and `INTERFACE_COMPILE_FEATURES` for the necessary C++11 flags. 4 5## External 6 7To use this library from a CMake project, you can locate it directly with `find_package()` and use the namespaced imported target from the generated package configuration: 8 9```cmake 10# CMakeLists.txt 11find_package(nlohmann_json 3.2.0 REQUIRED) 12... 13add_library(foo ...) 14... 15target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json) 16``` 17 18The package configuration file, `nlohmann_jsonConfig.cmake`, can be used either from an install tree or directly out of the build tree. 19 20## Embedded 21 22To embed the library directly into an existing CMake project, place the entire source tree in a subdirectory and call `add_subdirectory()` in your `CMakeLists.txt` file: 23 24```cmake 25# Typically you don't care so much for a third party library's tests to be 26# run from your own project's code. 27set(JSON_BuildTests OFF CACHE INTERNAL "") 28 29# If you only include this third party in PRIVATE source files, you do not 30# need to install it when your main project gets installed. 31# set(JSON_Install OFF CACHE INTERNAL "") 32 33# Don't use include(nlohmann_json/CMakeLists.txt) since that carries with it 34# unintended consequences that will break the build. It's generally 35# discouraged (although not necessarily well documented as such) to use 36# include(...) for pulling in other CMake projects anyways. 37add_subdirectory(nlohmann_json) 38... 39add_library(foo ...) 40... 41target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json) 42``` 43 44## Embedded (FetchContent) 45 46Since CMake v3.11, 47[FetchContent](https://cmake.org/cmake/help/v3.11/module/FetchContent.html) can 48be used to automatically download the repository as a dependency at configure type. 49 50Example: 51```cmake 52include(FetchContent) 53 54FetchContent_Declare(json 55 GIT_REPOSITORY https://github.com/nlohmann/json 56 GIT_TAG v3.7.3) 57 58FetchContent_GetProperties(json) 59if(NOT json_POPULATED) 60 FetchContent_Populate(json) 61 add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL) 62endif() 63 64target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json) 65``` 66 67!!! Note 68 The repository <https://github.com/nlohmann/json> download size is huge. 69 It contains all the dataset used for the benchmarks. You might want to depend on 70 a smaller repository. For instance, you might want to replace the URL above by 71 <https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent>. 72 73## Supporting Both 74 75To allow your project to support either an externally supplied or an embedded JSON library, you can use a pattern akin to the following: 76 77``` cmake 78# Top level CMakeLists.txt 79project(FOO) 80... 81option(FOO_USE_EXTERNAL_JSON "Use an external JSON library" OFF) 82... 83add_subdirectory(thirdparty) 84... 85add_library(foo ...) 86... 87# Note that the namespaced target will always be available regardless of the 88# import method 89target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json) 90``` 91```cmake 92# thirdparty/CMakeLists.txt 93... 94if(FOO_USE_EXTERNAL_JSON) 95 find_package(nlohmann_json 3.2.0 REQUIRED) 96else() 97 set(JSON_BuildTests OFF CACHE INTERNAL "") 98 add_subdirectory(nlohmann_json) 99endif() 100... 101``` 102 103`thirdparty/nlohmann_json` is then a complete copy of this source tree. 104