External Projects
Introduction
Sometimes, your project depends on external libraries or tools that aren’t part of your codebase. CMake provides two main ways to handle this:
ExternalProject_Add
: Downloads, builds, and integrates an external project during the build process.FetchContent
: Fetches and integrates external projects at configure time (introduced in CMake 3.11).
Key Concepts
FetchContent_Declare()
: Declares an external project (e.g., a GitHub repository).FetchContent_MakeAvailable()
: Downloads and makes the project available for use.- Integration: The external project’s targets can be used in your
CMakeLists.txt
as if they were part of your project.
Code Sample
- Let's start with a fresh CMakeLists.txt file to keep things simple. You can continue using the files from the previous chapter, but replace the entire contents of the CMakeLists.txt to the one below.
- Run cmake. This will download the googletest repo when it encounters
FetchContent_MakeAvailable
.
- Run the build