Cmake File Download [top] May 2026
downloads and builds dependencies during the build step . Because the files don't exist until you run make or ninja , you cannot use add_subdirectory() on them.
include(FetchContent) FetchContent_Declare( googletest URL https://github.com URL_HASH SHA256=8ad598c73696a0d039d9056960d65da577397334795018a47d25166fe6e0339d ) FetchContent_MakeAvailable(googletest) Use code with caution.
It makes the targets of the downloaded project visible to your main project at configuration time, enabling seamless use of target_link_libraries() . Basic Example: cmake file download
"Superbuilds," non-CMake projects (like OpenSSL or FFmpeg), and massive libraries that take a long time to compile.
GitHub repositories, shared libraries, and dependencies that you need to link against immediately. downloads and builds dependencies during the build step
It isolates the external build process, preventing the external project's variables from polluting your main project's scope. 3. file(DOWNLOAD) : Scripting and Simple Transfers file — CMake 4.3.2 Documentation
Downloading files with CMake is a critical task for modern C++ developers, whether you're fetching external libraries, pulling down test data, or ensuring your build environment is portable. Modern CMake provides several distinct mechanisms for downloading files, each optimized for a specific stage of the build lifecycle. It makes the targets of the downloaded project
Introduced in CMake 3.11, is now the preferred way to handle external dependencies. It downloads files during the configure step , which allows you to use the downloaded content immediately in your CMakeLists.txt via add_subdirectory() .