CPack Basics: TGZ & ZIP
CPack turns your install() rules into distributable packages: archives (.tar.gz, .zip), Linux packages (.deb, .rpm), Windows installers (NSIS, WiX .msi), macOS disk images. It works in two steps: it runs the install into a temporary staging directory (exactly like cmake --install with DESTDIR), then hands the staged files to a generator that builds the package.
That means packaging is only as good as your install rules. If cmake --install produces the right tree, CPack can package it.
Adding CPack to a project
Set a few CPACK_* variables, then include(CPack) at the end of the top-level CMakeLists.txt (after all install() calls, because it reads them):
set(CPACK_PACKAGE_VENDOR "TAD Tools")
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_GENERATOR "TGZ;ZIP")
include(CPack)Name, version and description default to the project() values (CPACK_PACKAGE_NAME, CPACK_PACKAGE_VERSION, CPACK_PACKAGE_DESCRIPTION_SUMMARY). include(CPack) writes CPackConfig.cmake and CPackSourceConfig.cmake into the build directory and adds the package and package_source targets.
mkdir -p tally/src && cd tally
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(tally VERSION 0.3.1 DESCRIPTION "Counts words in text" LANGUAGES CXX)
include(GNUInstallDirs)
add_executable(tally src/main.cpp)
install(TARGETS tally)
install(FILES README.md LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
set(CPACK_PACKAGE_VENDOR "TAD Tools")
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
set(CPACK_GENERATOR "TGZ;ZIP")
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_IGNORE_FILES "/build/;/\\\\.git/")
include(CPack)
CM
cat > src/main.cpp <<'SRC'
#include <iostream>
#include <string>
int main() { std::string w; int n = 0; while (std::cin >> w) ++n; std::cout << n << '\n'; }
SRC
echo '# tally' > README.md
echo 'MIT License' > LICENSE
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cd build
cpack
echo "--- contents"
tar -tzvf tally-0.3.1-Linux.tar.gz | awk '{print $1, $2, $4, $6}'
unzip -Z1 tally-0.3.1-Linux.zip
echo "--- source package"
cpack --config CPackSourceConfig.cmake > /dev/null
tar -tzf tally-0.3.1-Source.tar.gzCPack: Create package using TGZ
CPack: Install projects
CPack: - Install project: tally []
CPack: Create package
CPack: - package: /home/learner/tally/build/tally-0.3.1-Linux.tar.gz generated.
CPack: Create package using ZIP
CPack: Install projects
CPack: - Install project: tally []
CPack: Create package
CPack: - package: /home/learner/tally/build/tally-0.3.1-Linux.zip generated.
--- contents
drwxr-xr-x learner/learner 2026-01-01 tally-0.3.1-Linux/bin/
-rwxr-xr-x learner/learner 2026-01-01 tally-0.3.1-Linux/bin/tally
drwxr-xr-x learner/learner 2026-01-01 tally-0.3.1-Linux/share/
drwxr-xr-x learner/learner 2026-01-01 tally-0.3.1-Linux/share/doc/
drwxr-xr-x learner/learner 2026-01-01 tally-0.3.1-Linux/share/doc/tally/
-rw-r--r-- learner/learner 2026-01-01 tally-0.3.1-Linux/share/doc/tally/LICENSE
-rw-r--r-- learner/learner 2026-01-01 tally-0.3.1-Linux/share/doc/tally/README.md
tally-0.3.1-Linux/bin/
tally-0.3.1-Linux/bin/tally
tally-0.3.1-Linux/share/
tally-0.3.1-Linux/share/doc/
tally-0.3.1-Linux/share/doc/tally/
tally-0.3.1-Linux/share/doc/tally/LICENSE
tally-0.3.1-Linux/share/doc/tally/README.md
--- source package
tally-0.3.1-Source/CMakeLists.txt
tally-0.3.1-Source/LICENSE
tally-0.3.1-Source/README.md
tally-0.3.1-Source/src/
tally-0.3.1-Source/src/main.cpp
Notes:
cpackwith no arguments usesCPackConfig.cmakein the current directory and every generator inCPACK_GENERATOR.cpack -G TGZpicks one;cmake --build build --target packageis equivalent.- The archive contains a top-level directory
tally-0.3.1-Linux/(setCPACK_INCLUDE_TOPLEVEL_DIRECTORY OFFto drop it). The file name isCPACK_PACKAGE_FILE_NAME, by default<name>-<version>-<system>. - Archives record the user who ran CPack as the owner (
learner/learnerhere), which is harmless for archives people unpack into their own directories; the DEB generator in the next lesson usesroot/root. The timestamps inside come fromSOURCE_DATE_EPOCHwhen it is set (the course image sets it to 2026-01-01), a big step towards reproducible packages. - The source package is the source tree minus
CPACK_SOURCE_IGNORE_FILES(regexes, hence the escaped\\.git). Without the ignore list, it would include the build directory.
Build type matters
CPack packages whatever is in the build directory, so configure packaging builds as Release (or RelWithDebInfo). For multi-config generators, cpack -C Release selects the configuration; package presets ("configurations": ["Release"]) do the same.
Useful variables
| Variable | Purpose |
|---|---|
CPACK_PACKAGE_NAME, CPACK_PACKAGE_VERSION |
default to project() values |
CPACK_PACKAGE_FILE_NAME |
file name without extension |
CPACK_PACKAGE_DIRECTORY |
where packages are written (default: the build directory) |
CPACK_PACKAGING_INSTALL_PREFIX |
prefix inside the package (/usr for DEB/RPM by default; archives use none) |
CPACK_STRIP_FILES |
strip binaries before packaging |
CPACK_RESOURCE_FILE_LICENSE / README |
used by installers that show a licence page |
CPACK_THREADS |
parallel compression for archive generators |
Set them before include(CPack); per-generator variables (CPACK_DEBIAN_…, CPACK_RPM_…, CPACK_NSIS_…) follow the same rule. For values that differ per generator at packaging time, CPACK_PROJECT_CONFIG_FILE names a script CPack runs for each generator.
Test yourself
What does CPack package?
- Everything in the build directory
- The files your install() rules install, staged into a temporary directory
- The source directory
- Only executables
Where should include(CPack) go?
- Before project()
- At the end of the top-level CMakeLists.txt, after the install() rules and CPACK_* variables
- In each subdirectory
- In a toolchain file
How do you keep the build directory out of the source package?
- CPACK_STRIP_FILES
- CPACK_SOURCE_IGNORE_FILES with a regex such as /build/
- CPACK_PACKAGE_DIRECTORY
- It's excluded automatically