cpack: Package a CMake Project
cpack turns a CMake project's install rules into distributable packages, such as .tar.gz, .deb, .rpm, or an installer, using the selected generator.
CPack ships with CMake and reuses your install() rules to produce release artifacts. In CI it is the step after cmake --install when you want a downloadable package rather than a staged tree.
What it does
cpack reads CPackConfig.cmake (generated by include(CPack) in your project) and produces packages with the chosen generator: TGZ for tarballs, DEB and RPM for Linux packages, NSIS or WIX for Windows installers, DragNDrop for macOS.
Common usage
cpack -G TGZ --config build/CPackConfig.cmake
cd build && cpack -G "DEB;RPM"
cpack -G TGZ -C ReleaseOptions
| Flag | What it does |
|---|---|
| -G <generator> | Package format: TGZ, ZIP, DEB, RPM, NSIS, WIX, DragNDrop |
| --config <file> | CPack config file to use (CPackConfig.cmake) |
| -C <cfg> | Multi-config: which build configuration to package |
| -B <dir> | Directory to write the packages into |
| -D <var>=<value> | Override a CPack variable |
In CI
Run cpack from the build directory after a successful build so it packages fresh artifacts. DEB and RPM generators need packaging tools present (dpkg-deb for DEB, rpmbuild for RPM); install them in the runner image. Set CPACK_PACKAGE_VERSION from your tag so artifacts are versioned.
Common errors in CI
"CPack Error: Cannot find CPack config file: CPackConfig.cmake" means include(CPack) was not in the project or you ran from the wrong directory. "CPack Error: Cannot find a suitable generator" means the requested -G is unknown or its tool is missing (e.g. no rpmbuild for RPM). "CPack Error: Problem creating installer" usually surfaces a missing packaging binary just above it.