nlohmann Jsonのcmake設定
はじめに
C++でJsonを読み書きするライブラリであるnlohmann-jsonを cmakeで取り込む方法を記載する。
方法
基本的にnlohmann-jsonのreadmeに
書かれている通りに記載する。
ただし、そのままではnlohmann/json.hpp
で取り込めないためinclude_directories
で
ヘッダーファイルの場所を指定する。
# use FetchContent to download
include(FetchContent)
# fetch library
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
FetchContent_MakeAvailable(json)
include_directories(${json_SOURCE_DIR}/include) # append to include header
# link
target_include_directories(hoge PUBLIC nlohmann_json::nlohmann_json)
すると次のようにインクルードできる。
#include <nlohmann/json.hpp>