TExcelTAD
Home › Modern CMake (with CTest & CPack) › Exporting Targets

Exporting Targets

Installing a library's files is half the job. The other half is telling other CMake projects how to use them: which file is the library, which include directory, which compile features, which dependencies. CMake writes that description for you as an export file: a generated .cmake script that recreates your targets as imported targets on the consumer's side.

There are two kinds:

Command Writes Points at Used for
install(EXPORT …) <prefix>/lib/cmake/<Pkg>/<Pkg>Targets.cmake at install time the installed files packages, SDKs, system installs
export(EXPORT …) <build>/<Pkg>Targets.cmake at configure time the build tree using a library straight from another project's build directory, without installing

Step 1: add targets to an export set

install(TARGETS … EXPORT <set>) installs the files and records the targets in a named export set:

cmake
install(TARGETS greet EXPORT GreetTargets)

Step 2: write the export set out

cmake
install(EXPORT GreetTargets
  NAMESPACE Greet::                                  # consumers see Greet::greet
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Greet)

The whole thing, and what it generates

bash · try it yourself
mkdir -p greet/include/greet greet/src && cd greet
cat > include/greet/greet.hpp <<'SRC'
#pragma once
#include <string>
namespace greet { std::string hello(const std::string& who); }
SRC
cat > src/greet.cpp <<'SRC'
#include <greet/greet.hpp>
std::string greet::hello(const std::string& who) { return "Hello, " + who + "!"; }
SRC
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet VERSION 1.4.0 LANGUAGES CXX)
include(GNUInstallDirs)

add_library(greet src/greet.cpp)
add_library(Greet::greet ALIAS greet)
target_include_directories(greet PUBLIC
  
lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
lt;INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) target_compile_features(greet PUBLIC cxx_std_17) install(TARGETS greet EXPORT GreetTargets) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT GreetTargets NAMESPACE Greet:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Greet) export(EXPORT GreetTargets NAMESPACE Greet:: FILE ${CMAKE_CURRENT_BINARY_DIR}/GreetTargets.cmake) CM cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null cmake --build build > /dev/null cmake --install build --prefix "$HOME/sdk" | grep cmake/Greet echo "--- the interesting part of GreetTargets.cmake" grep -v '^#' ~/sdk/lib/cmake/Greet/GreetTargets.cmake | sed -n '/^add_library/,/^)/p' echo "--- GreetTargets-release.cmake" grep -v -e '^#' -e '^ ~/sdk/lib/cmake/Greet/GreetTargets-release.cmake | sed -n '/IMPORTED_CONFIGURATIONS/,/^ )/p' echo "--- the build-tree export" grep -A3 '^set_target_properties' build/GreetTargets.cmake
-- Installing: /home/learner/sdk/lib/cmake/Greet/GreetTargets.cmake
-- Installing: /home/learner/sdk/lib/cmake/Greet/GreetTargets-release.cmake
--- the interesting part of GreetTargets.cmake
add_library(Greet::greet STATIC IMPORTED)

set_target_properties(Greet::greet PROPERTIES
  INTERFACE_COMPILE_FEATURES "cxx_std_17"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
--- GreetTargets-release.cmake
set_property(TARGET Greet::greet APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(Greet::greet PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libgreet.a"
  )
--- the build-tree export
set_target_properties(Greet::greet PROPERTIES
  INTERFACE_COMPILE_FEATURES "cxx_std_17"
  INTERFACE_INCLUDE_DIRECTORIES "/home/learner/greet/include"
)
--
set_target_properties(Greet::greet PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  IMPORTED_LOCATION_RELEASE "/home/learner/greet/build/libgreet.a"
  )

Look at the generated code:

  • add_library(Greet::greet STATIC IMPORTED): the consumer gets an imported target with the namespaced name.
  • INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include": the path is computed relative to the file's own location (_IMPORT_PREFIX is three directories up from lib/cmake/Greet), so the installed package can be moved.
  • INTERFACE_COMPILE_FEATURES "cxx_std_17": the PUBLIC usage requirements were carried over.
  • The per-configuration file sets IMPORTED_LOCATION_RELEASE. Installing a Debug build too adds GreetTargets-debug.cmake next to it, and consumers pick the matching one automatically.
  • The build-tree export points at the source and build directories instead (/home/learner/greet/include): that's the
    lt;BUILD_INTERFACE:…>
    half of the include directory; the next lessons explain the pair.

Consuming the export file directly

A consumer could now do this (but read on):

cmake
include(/home/learner/sdk/lib/cmake/Greet/GreetTargets.cmake)
target_link_libraries(app PRIVATE Greet::greet)

It works, but it skips everything find_package gives you: version checks, a standard search path, Greet_FOUND, and finding the package's own dependencies. The export file is meant to be included by a package config file, GreetConfig.cmake. That's the next lesson.

Export sets must be complete

If an exported target links another target of yours, that target must be in an export set too, or CMake refuses to generate:

bash · try it yourself
mkdir -p broken && cd broken
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Broken LANGUAGES CXX)
add_library(util STATIC util.cpp)
add_library(core STATIC core.cpp)
target_link_libraries(core PUBLIC util)
install(TARGETS core EXPORT CoreTargets)     # util is missing
install(EXPORT CoreTargets DESTINATION lib/cmake/Core)
CM
echo 'int util() { return 1; }' > util.cpp
echo 'int core() { return 2; }' > core.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE
-- Configuring done (0.2s)
CMake Error in CMakeLists.txt:
  install(EXPORT "CoreTargets" ...) includes target "core" which requires
  target "util" that is not in any export set.


-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

The fix is install(TARGETS core util EXPORT CoreTargets). Dependencies found with find_package (imported targets) don't need to be exported; they must be found again by the consumer, which is what find_dependency() in the config file is for.

Test yourself

What does install(EXPORT GreetTargets NAMESPACE Greet:: ...) generate?

  1. A static library
  2. A .cmake file that recreates the exported targets as imported targets named Greet::...
  3. A pkg-config file
  4. A CMakePresets.json

What is export(EXPORT ...) (without install) for?

  1. Exporting environment variables
  2. Writing an export file into the build tree so another project can use the library without installing it
  3. Uploading a package
  4. Exporting compile_commands.json

An exported library links another library from the same project that is not in any export set. What happens?

  1. It works
  2. CMake reports that the target requires a target that is not in any export set
  3. The dependency is silently dropped
  4. The dependency is installed automatically