When you work on a project, you often rely on external libraries and source code. If you use CMake as your build tool: it can download the artifacts for you. And make them available during build. By using FetchContent.
use case: your project needs to invoke a callback handler. This is a common case when you work with interrupts or event driven designs. You want to use an open source module that’s available on GitHub. |
You tell CMake that you rely on a resource, and it will take care that the resource is available for your build process. There are a number of services that are supported. In this blog, I’m using a GitHub repository as example.
include(FetchContent)
FetchContent_Declare(callbackmanager
GIT_REPOSITORY "https://github.com/jancumps/callbackmanager.git"
GIT_TAG "origin/main"
SOURCE_SUBDIR =
)
FetchContent_MakeAvailable(callbackmanager)
# sources now available at ${callbackmanager_SOURCE_DIR}
# . . .
target_sources(${CMAKE_PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
target_sources(${CMAKE_PROJECT_NAME}
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES FILES
${callbackmanager_SOURCE_DIR}/callbackmanager.cpp
)
# . . .
You can configure how deep the integration with your build is:
-
just download the sources to a known destination,
-
also have CMake expose them in your build process
-
or even let CMake build those dependencies (e.g.: a library) before your build runs.
In this post, I’m using the second option. I retrieve the a GitHub repository right before build, and use the sources in the build process.
The 3rd option, where CMake also builds the fetched content, is used by Google Test. This Unit Test library has advanced integration. They take care that their GitHub repository is not just fetched. But their libraries are built and ready to get linked with your project. It’s a good examle of getting the most out of this construct.
There are many ways to link dependencies to your project. A construct that I have been using, is the GIT submodule. GIT allows you to get the content of other repositories inside your project, by adding them as a submodule of your repo. I tend to use that when the dependencies (libraries) are closely tied to the project, and I want to develop them together with the whole project. Also when I want tight IDE coding support.
|