install() & GNUInstallDirs
Building produces files scattered over a build tree. Installing copies the ones users need (executables, libraries, headers, data) into a clean directory layout under an install prefix, such as /usr/local, /opt/myapp or a staging folder that a package is made from.
You describe what to install with install() rules in CMakeLists.txt, and run them with cmake --install:
cmake --install build # into CMAKE_INSTALL_PREFIX (default /usr/local)
cmake --install build --prefix "$HOME/sdk" # choose the prefix at install time
cmake --install build --component runtime # only one component
DESTDIR=/tmp/stage cmake --install build # staged install: /tmp/stage/usr/local/...GNUInstallDirs: the standard layout
Don't hard-code lib or include. The GNUInstallDirs module defines the conventional relative directories, adjusted for the platform (for example lib64 on some Linux distributions, or lib/x86_64-linux-gnu on Debian when the prefix is /usr):
mkdir -p dirs && cd dirs
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Dirs LANGUAGES C) # GNUInstallDirs needs a language to pick LIBDIR
include(GNUInstallDirs)
foreach(d BINDIR LIBDIR INCLUDEDIR DATADIR DOCDIR SYSCONFDIR MANDIR)
message("CMAKE_INSTALL_${d} = ${CMAKE_INSTALL_${d}}")
endforeach()
message("CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")
CM
cmake -S . -B build --log-level=NOTICE | grep -v '^--'CMAKE_INSTALL_BINDIR = bin
CMAKE_INSTALL_LIBDIR = lib
CMAKE_INSTALL_INCLUDEDIR = include
CMAKE_INSTALL_DATADIR = share
CMAKE_INSTALL_DOCDIR = share/doc/Dirs
CMAKE_INSTALL_SYSCONFDIR = etc
CMAKE_INSTALL_MANDIR = share/man
CMAKE_INSTALL_PREFIX = /usr/local
LIBDIR depends on the target architecture, so include the module after project() has enabled a language (with LANGUAGES NONE it warns and falls back to lib). The values are relative (except SYSCONFDIR for some prefixes), so the same rules work for any prefix. Each one is a cache variable, so a distribution packager can override it (-DCMAKE_INSTALL_LIBDIR=lib64).
Installing targets, headers and files
mkdir -p tally/include/tally tally/src && cd tally
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Tally VERSION 1.0.0 LANGUAGES CXX)
include(GNUInstallDirs)
add_library(tally src/tally.cpp)
target_include_directories(tally PUBLIC include)
add_executable(tally-cli src/main.cpp)
target_link_libraries(tally-cli PRIVATE tally)
install(TARGETS tally tally-cli
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # executables (and DLLs on Windows)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so / .dylib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) # .a / .lib
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(PROGRAMS scripts/tally-report DESTINATION ${CMAKE_INSTALL_BINDIR})
CM
echo '#pragma once
int count_words(const char* s);' > include/tally/tally.hpp
cat > src/tally.cpp <<'SRC'
#include <tally/tally.hpp>
int count_words(const char* s) {
int n = 0; bool in = false;
for (; *s; ++s) { bool sp = *s == ' '; if (!sp && !in) ++n; in = !sp; }
return n;
}
SRC
cat > src/main.cpp <<'SRC'
#include <cstdio>
#include <tally/tally.hpp>
int main(int argc, char** argv) { std::printf("%d\n", count_words(argc > 1 ? argv[1] : "")); }
SRC
echo '# Tally' > README.md
mkdir -p scripts && printf '#!/bin/sh\necho "tally report"\n' > scripts/tally-report
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/stage"
cd ~ && tree stage
stage/bin/tally-cli "three little words"
cat tally/build/install_manifest.txt | sed "s#$HOME#~#"-- Install configuration: "Release"
-- Installing: /home/learner/stage/lib/libtally.a
-- Installing: /home/learner/stage/bin/tally-cli
-- Installing: /home/learner/stage/include
-- Installing: /home/learner/stage/include/tally
-- Installing: /home/learner/stage/include/tally/tally.hpp
-- Installing: /home/learner/stage/share/doc/Tally/README.md
-- Installing: /home/learner/stage/bin/tally-report
stage
├── bin
│ ├── tally-cli
│ └── tally-report
├── include
│ └── tally
│ └── tally.hpp
├── lib
│ └── libtally.a
└── share
└── doc
└── Tally
└── README.md
8 directories, 5 files
3
~/stage/lib/libtally.a
~/stage/bin/tally-cli
~/stage/include/tally/tally.hpp
~/stage/share/doc/Tally/README.md
~/stage/bin/tally-report
Notes on what you just saw:
install(TARGETS …)copies the build output, whatever it is called on the platform (tally-cli.exeon Windows). Since CMake 3.14 the destinations default to the GNUInstallDirs values, so the threeDESTINATIONlines are optional; many projects keep them for clarity.install(DIRECTORY include/ …)with the trailing slash copies the contents ofinclude/; without it you would getinclude/include/tally/….PROGRAMSinstalls a file with execute permission;FILESwithout.install_manifest.txtin the build directory lists every installed file, which is how you can uninstall by hand (xargs rm < build/install_manifest.txt). CMake has nouninstalltarget; packages (CPack) are the clean way to install and remove.- The install step prints
-- Installing:for new files and-- Up-to-date:for files that haven't changed.
Components
Each rule can be tagged with a COMPONENT, so one project can install (and later package) a runtime part and a development part separately:
install(TARGETS tally-cli RUNTIME COMPONENT runtime)
install(TARGETS tally ARCHIVE COMPONENT dev LIBRARY COMPONENT runtime)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT dev)cmake --install build --component runtime then installs only the runtime files. The CPack components lesson turns components into separate .deb packages.
Staged installs with DESTDIR
The install prefix is compiled into some files (for example, the RPATH, or paths in config files), so packagers configure with the final prefix and install into a temporary root with DESTDIR:
mkdir -p hello && cd hello
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Hello LANGUAGES CXX)
add_executable(hello main.cpp)
install(TARGETS hello)
CM
echo 'int main() { return 0; }' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_INSTALL_PREFIX=/usr > /dev/null
cmake --build build > /dev/null
DESTDIR=$HOME/pkgroot cmake --install build-- Install configuration: ""
-- Installing: /home/learner/pkgroot/usr/bin/hello
Nothing touched /usr; the files are in ~/pkgroot/usr/bin, ready to be archived into a package. That is exactly what CPack does internally.
Test yourself
What does install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install, given include/mylib/a.hpp?
- <prefix>/include/include/mylib/a.hpp
- <prefix>/include/mylib/a.hpp
- <prefix>/mylib/a.hpp
- Nothing, DIRECTORY needs FILES_MATCHING
Why use ${CMAKE_INSTALL_LIBDIR} instead of lib?
- It's shorter
- It follows the platform's conventions (lib64, multiarch) and packagers can override it
- lib is reserved
- It is required by install(TARGETS)
What is DESTDIR for?
- Choosing the install prefix
- Installing into a staging root while keeping the configured prefix (for packaging)
- Selecting a component
- Changing the build directory