BUILD_INTERFACE, INSTALL_INTERFACE & File Sets
A library's headers live in two places during its life: in the source tree while it is being built (/home/ada/greet/include), and under the install prefix once installed (<prefix>/include). Consumers in the same build need the first path; consumers of the installed package need the second. Two generator expressions say which is which:
target_include_directories(greet PUBLIC
lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> # used inside this build (and export())
lt;INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) # written into the installed GreetTargets.cmakelt;BUILD_INTERFACE:…>is kept for targets in the build tree and forexport(), and removed byinstall(EXPORT).lt;INSTALL_INTERFACE:…>is the opposite. A relative path inside it is relative to the install prefix, which keeps the package relocatable.
What happens without them
Exporting a source-tree path into an installed package would make every consumer depend on your source directory. CMake refuses:
mkdir -p greet/include/greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet LANGUAGES CXX)
add_library(greet greet.cpp)
target_include_directories(greet PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
install(TARGETS greet EXPORT GreetTargets)
install(EXPORT GreetTargets DESTINATION lib/cmake/Greet)
CM
echo 'int greet() { return 0; }' > greet.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE-- Configuring done (0.2s)
CMake Error in CMakeLists.txt:
Target "greet" INTERFACE_INCLUDE_DIRECTORIES property contains path:
"/home/learner/greet/include"
which is prefixed in the source directory.
-- Generating done (0.0s)
CMake Generate step failed. Build files cannot be regenerated correctly.
The message says it all: the path is "prefixed in the source directory". The two generator expressions are the fix. If the library also has generated headers in the build directory (an export header, a config.h from configure_file), list that directory in lt;BUILD_INTERFACE:…> as well and install the generated files next to the others.
Comparing the two exports
mkdir -p greet/include/greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet LANGUAGES CXX)
include(GNUInstallDirs)
add_library(greet greet.cpp)
target_include_directories(greet PUBLIC
lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
lt;BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
lt;INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
install(TARGETS greet EXPORT GreetTargets)
install(EXPORT GreetTargets NAMESPACE Greet:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Greet)
export(EXPORT GreetTargets NAMESPACE Greet:: FILE GreetTargets.cmake)
CM
echo 'int greet() { return 0; }' > greet.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null && cmake --install build --prefix "$HOME/sdk" > /dev/null
echo "build tree: $(grep INTERFACE_INCLUDE_DIRECTORIES build/GreetTargets.cmake)"
echo "install tree: $(grep INTERFACE_INCLUDE_DIRECTORIES ~/sdk/lib/cmake/Greet/GreetTargets.cmake)"build tree: INTERFACE_INCLUDE_DIRECTORIES "/home/learner/greet/include;/home/learner/greet/build/generated"
install tree: INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
The modern alternative: FILE_SET HEADERS
Since CMake 3.23, a target can own its headers as a file set. One declaration does three jobs: it adds the base directory to the include path (with the right BUILD/INSTALL split automatically), it lists the headers as part of the target, and install(TARGETS … FILE_SET HEADERS) installs them with their directory structure:
mkdir -p greet/include/greet/detail && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet LANGUAGES CXX)
include(GNUInstallDirs)
add_library(greet greet.cpp)
target_sources(greet PUBLIC
FILE_SET HEADERS
BASE_DIRS include
FILES include/greet/greet.hpp include/greet/detail/format.hpp)
install(TARGETS greet EXPORT GreetTargets FILE_SET HEADERS)
install(EXPORT GreetTargets NAMESPACE Greet:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Greet)
CM
echo '#pragma once
#include "detail/format.hpp"' > include/greet/greet.hpp
echo '#pragma once' > include/greet/detail/format.hpp
echo '#include <greet/greet.hpp>
int greet() { return 0; }' > greet.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null
cmake --install build --prefix "$HOME/sdk"
grep -B1 -A2 "APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES" ~/sdk/lib/cmake/Greet/GreetTargets.cmake-- Install configuration: ""
-- Installing: /home/learner/sdk/lib/libgreet.a
-- Installing: /home/learner/sdk/include/greet/greet.hpp
-- Installing: /home/learner/sdk/include/greet/detail/format.hpp
-- Installing: /home/learner/sdk/lib/cmake/Greet/GreetTargets.cmake
-- Installing: /home/learner/sdk/lib/cmake/Greet/GreetTargets-noconfig.cmake
set_property(TARGET Greet::greet
APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
"${_IMPORT_PREFIX}/include"
)
No generator expressions and no separate install(DIRECTORY …): the file set knows that include/ is the base, so the installed layout keeps greet/detail/format.hpp, and only the headers you listed are installed (no stray editor backups or private headers). Use FILE_SET HEADERS in new projects that can require CMake 3.23.
Other uses of the pair
The same expressions work in any usage requirement, for example a definition only consumers of the installed package should see:
target_compile_definitions(greet PUBLIC lt;INSTALL_INTERFACE:GREET_INSTALLED=1>)And lt;INSTALL_PREFIX> expands to the install prefix inside lt;INSTALL_INTERFACE:…>, for the rare case where an absolute installed path is required.
Test yourself
Why can't an installed package's INTERFACE_INCLUDE_DIRECTORIES contain /home/ada/greet/include?
- Paths must be relative in CMake
- Consumers would depend on the library author's source directory; CMake rejects source-tree paths in install(EXPORT)
- Absolute paths are slower
- It only fails on Windows
A relative path inside lt;INSTALL_INTERFACE:...> is relative to what?
- The source directory
- The build directory
- The install prefix of the package (wherever it ends up)
- The current working directory
What does target_sources(lib PUBLIC FILE_SET HEADERS BASE_DIRS include FILES ...) give you?
- Only IDE grouping
- The include path for build and install, the header list, and installation via install(TARGETS ... FILE_SET HEADERS)
- Precompiled headers
- Header-only compilation