TExcelTAD
Home › Modern CMake (with CTest & CPack) › CPack Basics: TGZ & ZIP

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):

cmake
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.

bash · try it yourself
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.gz
CPack: 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:

  • cpack with no arguments uses CPackConfig.cmake in the current directory and every generator in CPACK_GENERATOR. cpack -G TGZ picks one; cmake --build build --target package is equivalent.
  • The archive contains a top-level directory tally-0.3.1-Linux/ (set CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF to drop it). The file name is CPACK_PACKAGE_FILE_NAME, by default <name>-<version>-<system>.
  • Archives record the user who ran CPack as the owner (learner/learner here), which is harmless for archives people unpack into their own directories; the DEB generator in the next lesson uses root/root. The timestamps inside come from SOURCE_DATE_EPOCH when 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?

  1. Everything in the build directory
  2. The files your install() rules install, staged into a temporary directory
  3. The source directory
  4. Only executables

Where should include(CPack) go?

  1. Before project()
  2. At the end of the top-level CMakeLists.txt, after the install() rules and CPACK_* variables
  3. In each subdirectory
  4. In a toolchain file

How do you keep the build directory out of the source package?

  1. CPACK_STRIP_FILES
  2. CPACK_SOURCE_IGNORE_FILES with a regex such as /build/
  3. CPACK_PACKAGE_DIRECTORY
  4. It's excluded automatically