Include the right version right in your repo; provide a custom command to update CPM.cmake — example

Setup cmake_modules path — example CMake

Best Practices

  • Avoid short hand
  • Keep package dependencies with their targets

CMake is a cross-platform build system that can be used to build, test, and package software projects. One of the most powerful features of CMake is the ability to manage dependencies, and using CMake Package Manager (CPM) make that a breeze. CPM is a package manager for CMake that allows you to easily download and use libraries and other dependencies in your project without having to manually manage them.

Using CPM effectively can greatly simplify the process of managing dependencies in your project. Here are a few tips to help you get the most out of CPM:

  1. Start by adding CPM to your project. This can be done by adding the following line to the top of your CMakeLists.txt file (note that you will need to have a cmake/CPM.cmake in that path relative to your CMakeLists.txt file). You can find up-to-date versions of CPM.cmake here (documentation is here).
include(cmake/CPM.cmake)Code language: PHP (php)
  1. Next, specify the dependencies you need for your project by adding CPMAddPackage commands. For example, to add the msgpack-c library, you would add the following stanza:
CPMAddPackage(
  NAME msgpack
  GIT_TAG cpp-4.1.1
  GITHUB_REPOSITORY "msgpack/msgpack-c"
  OPTIONS "MSGPACK_BUILD_DOCS OFF" "MSGPACK_CXX20 ON" "MSGPACK_USE_BOOST OFF"
)
Code language: JavaScript (javascript)
  1. Once you have added all the dependencies you require, you can use them in your project by including the appropriate headers and linking against the appropriate libraries. Note that CPM will pull the dependency from the repository specified and then run CMake (if the project uses CMake to build). Because CMake is run for the project, the CMake targets are available for you to use. For example, to use the msgpack-c library in your project, you would add the following lines to your CMakeLists.txt file:
target_link_libraries(your_target msgpackc-cxx)
  1. CPM also allows you to specify options for modules, such as disabling tests or building in a specific configuration. To specify options for a module, you can use the OPTIONS argument, as shown above.
  2. When the dependency does not have a CMakeLists.txt file, CPM will still checkout the repository, but will not configure it. In that case, you are required to write your own CMake to perform the build as required. For example, the embedded web server called Mongoose from Cesanta does not provide a CMakeLists.txt to build it, but we can still pull it in like this (note the use of the CPM generated variable mongoose_SOURCE_DIR):
CPMAddPackage(
  NAME mongoose
  GIT_TAG 7.8
  GITHUB_REPOSITORY "cesanta/mongoose"
)
if (mongoose_ADDED)
  add_library(mongoose SHARED ${mongoose_SOURCE_DIR}/mongoose.c)
  target_include_directories(mongoose SYSTEM PUBLIC ${mongoose_SOURCE_DIR})
  target_compile_definitions(mongoose PUBLIC MG_ENABLE_OPENSSL)
  target_link_libraries(mongoose PUBLIC ssl crypto)
  install(TARGETS mongoose)
endif(mongoose_ADDED)Code language: PHP (php)
  1. Add dependencies with CPM in the same CMakeLists.txt as the target that uses the dependency. If multiple targets use the same dependency, CPM will not pull multiple copies, rather it will use the copy already downloaded. By doing this, you ensure that if you ever refactor your CMake, or pull a CMakeLists.txt for a module, you get all the dependencies and don’t miss anything.

CPM is a powerful tool that can help simplify the process of managing dependencies in your project. By following the tips outlined in this blog, you can effectively use CPM to manage your dependencies, ensuring that your project is always up-to-date and easing the burden of keeping your dependencies up-to-date as well. With the right approach, CPM can help you save time and effort when managing your project dependencies, allowing you to focus on building and delivering your project.

Last modified: January 30, 2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.