Creating Reusable Packages
Understand the process of creating reusable C++ packages with CMake by making targets relocatable, generating and installing target export files, and building config files for seamless project integration and consumption.
We'll cover the following...
We have used find_package() extensively. We saw how convenient it is and how it simplifies the whole process. To make our project accessible through this command, we need to complete a few steps so that CMake can treat our project as a coherent package:
Make our targets relocatable.
Install the target export file to a standard location.
Create a config-files and version file for the package.
Let's start from the beginning: why do targets need to be relocatable, and how can we do this?
Understanding the issues with relocatable targets
Installation solves many problems, but unfortunately, it also introduces some complexity: not only is CMAKE_INSTALL_PREFIX platform-specific, but it can also be set by the user at the installation stage with the --prefix option. However, target export files are generated before the installation and during the build stage, at which point we don't know where the installed artifacts will go. Take a look at the following code:
In this example, we specifically add the include directory ...