Cmake修改FetchContent_Declare为本地代码构建

在构建代码时,某些项目可能需要额外下载第三方库。然而,由于网络不稳定,克隆过程可能会出现问题。在这种情况下,您需要手动修改项目配置,将其指向已经下载到本地的库文件。
请添加图片描述

Fetch doctest.
message(STATUS "Fetching doctest")
include(FetchContent)
FetchContent_Declare(doctest URL https://github.com/doctest/doctest/archive/refs/tags/v2.4.9.zip)
FetchContent_MakeAvailable(doctest)

add_subdirectory是CMake中的一个指令,用于将其他目录(通常包含一个CMakeLists.txt文件)添加到当前构建系统中。这使得您可以将项目分割成多个子目录,并在这些子目录中组织构建目标和源代码。

add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])

添加了add_subdirectory来引入doctest的源文件和构建文件。但是您需要将doctest的头文件路径添加到foonathan_memory_test的包含路径中。

在这里,您需要在 target_link_libraries 函数后添加 target_include_directories 函数:
target_link_libraries(foonathan_memory_test PRIVATE foonathan_memory)
target_include_directories(foonathan_memory_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/…/_deps/doctest-src
${FOONATHAN_MEMORY_SOURCE_DIR}/include/foonathan/memory)

猜你喜欢

转载自blog.csdn.net/qq_37464479/article/details/129752269