我有一个需要使用两个库的小项目,即 ASIO(独立)和 nlohmann/json。
将 json 库添加到我的 CMake 项目非常简单:
include(FetchContent)
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.9.1)
FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)
Run Code Online (Sandbox Code Playgroud)
但是,由于 ASIO 没有 CMakeLists.txt,它似乎不起作用。是否有解决方法,或者我是否绝对需要在外部安装 ASIO?
可能值得一提的是,这两个库都是仅头文件,所以我只需要 cmake 来获取库并添加适当的包含路径。