C++ doesn’t have a default package manager, making the development with external libraries complex, especially if you’re making a multiplatform system. Because of this absence, the community has created some strategies to work around and there are even some pretty consolidated package managers out there, such as Conan and Vcpkg, but none of them are simple and easy to use like CPM (no, we aren’t talking about this one).

CPM is the new kid on the block, its first version was released on April 2019, it’s a CMake script that gives CMake the capacity to make version control, caching, and install the dependencies of your project, created by Lars Melchior.

Usually, on a modern C++ and CMake project, the libraries are installed on the system and** find_package()** is called to load the library configuration, forcing every developer working on the project to have the same environment (sometimes with different tools’ versions, giving us a nasty headache).

CPM has a simple syntax and it’s pretty straightforward, making our lives as devs way easier. We call CPMAddPackage to install an external library (or CPMFindPackage to search the system, and, if it’s not found, install it). There are two ways to download a library: through a Github repository or a link. Let’s use Nlohmann’s JSON library as an example:

All right, to understand the simplicity and ease of CPM let’s create a small project and do a quick example using spdlog, made by Gabi Melman.

This is how the source tree will look like:

src
|___main.cpp
CMakeLists.txt

This is our CMakeLists.txt:

First, we have to download CPM’s script on our project and include it on CMake. There are two ways to do this. The first one is manually downloading it with the following commands:

$ mkdir -p cmake

$ wget -O cmake/CPM.cmake 

https://github.com/TheLartians/CPM.cmake/releases/latest/download/CPM.cmake

#cpm #cmake #dependency-manager #package-manager #cpp #cplusplus

CPM: An Awesome Dependency Manager for C++ With CMake
17.55 GEEK