
Background Introduction
In the realm of C++ package management tools, aside from Microsoft’s vcpkg, there doesn’t seem to be a particularly famous package manager. CMake actually provides basic package management functionality. By using the <span>FetchContent</span> module commands, you can download the source code or other files that your project depends on.
Basic Usage
<span>FetchContent_Declare</span> command defines the content we want to download, supporting retrieval from URL, GIT, SVN, Mercurial, or CVS. Taking the commonly used GIT and URL as examples, the basic usage is as follows:
cmake_minimum_required(VERSION 3.11)include(FetchContent)
FetchContent_Declare( googletest # Case-sensitive non-empty string representing the name of this dependency, used in subsequent find_package, recommended to use the official project name GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # release-1.15.2)FetchContent_Declare( myCompanyIcons URL https://intranet.mycompany.com/assets/iconset_1.12.tar.gz URL_HASH MD5=5588a7b18261c20068beabfb4f530b87)
FetchContent_MakeAvailable(googletest myCompanyIcons)
find_package(googletest)find_package(myCompanyIcons)
You need to call FetchContent_MakeAvailable to execute the actual download task, ensuring that the libraries defined in FetchContent_Declare can be used by the current build system.
FetchContent_MakeAvailable(<name1> [<name2>...])
This way, you can directly use the find_package command to reference these dependencies in subsequent CMake scripts.
Advanced Usage
<span>FetchContent</span> module provides several useful variables, <span>FETCHCONTENT_BASE_DIR</span>, <span>FETCHCONTENT_QUIET</span>, and <span>FETCHCONTENT_FULLY_DISCONNECTED</span>.
FETCHCONTENT_BASE_DIR sets the directory where downloads are saved, defaulting to <span>${CMAKE_BINARY_DIR}/_deps</span>. We can use this variable to customize the download save directory.
set(FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/third_party)
FETCHCONTENT_QUIET controls whether detailed logs are displayed during the download process. If we encounter download failures, we can enable this switch to facilitate troubleshooting. The default is off.
set(FETCHCONTENT_QUIET ON)
FETCHCONTENT_FULLY_DISCONNECTED
Controls whether to re-download dependencies on each build. When set to ON, it assumes that the dependencies were correctly used in the last build, and the developer knows that these dependencies' versions have not changed, so re-downloading is not necessary for subsequent builds. The default is to re-download on every build, which is OFF.
set(FETCHCONTENT_FULLY_DISCONNECTED ON)
When using FetchContent_Declare, the default CMakeLists.txt file for the dependency project is expected to be in its root directory. If this is not the case, for example, in the well-known protobuf project, where its CMakeLists.txt is located in the cmake directory of its root, we can use the SOURCE_SUBDIR parameter to specify the path to the CMakeLists.txt, telling FetchContent_Declare where to find it.
FetchContent_Declare( protobuf GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git GIT_TAG 1be1c9d0ea6efa2a25bd7b76186844d1669be78a # v29.4 SOURCE_SUBDIR cmake)
References
https://cmake.org/cmake/help/latest/module/ExternalProject.htmlhttps://cmake.org/cmake/help/latest/module/FetchContent.html