Exporting without installation
How can we make the targets of project A
available to the consuming project, B
? Usually, we’d use the find_package()
command, but that requires creating a package and installing it on the system. While useful, this approach involves some work. Sometimes, we just need a quick way to build a project and make its targets available for other projects.
One time-saving method is to include in project B
the main listfile of A
, which already contains all the target definitions. However, this file might also include global configuration, CMake commands with side effects, additional dependencies, and perhaps unwanted targets for B
(like unit tests). So, this is not the best approach. Instead, we can provide a target export file for the consuming project, B
, to include with the include()
command:
cmake_minimum_required(VERSION 3.26.0)
project(B)
include(/path/to/A/TargetsOfA.cmake)
This will define all targets of A
with the correct properties...