DEB Packages
The DEB generator builds Debian/Ubuntu packages directly. It doesn't need debhelper or a debian/ directory: CPack writes the control file and the archive itself, so a .deb can be produced on any Linux machine (even inside CI containers that aren't Debian-based), and inspected with dpkg-deb.
The variables that matter
| Variable | Control field / effect |
|---|---|
CPACK_DEBIAN_PACKAGE_MAINTAINER (or CPACK_PACKAGE_CONTACT) |
Maintainer: (required) |
CPACK_DEBIAN_PACKAGE_NAME |
Package: (defaults to the lower-cased project name) |
CPACK_DEBIAN_PACKAGE_VERSION, CPACK_DEBIAN_PACKAGE_RELEASE |
Version: (0.3.1-1 with a release number) |
CPACK_DEBIAN_PACKAGE_ARCHITECTURE |
Architecture: (default from dpkg --print-architecture) |
CPACK_DEBIAN_PACKAGE_DEPENDS |
Depends: written by hand, e.g. libc6 (>= 2.34) |
CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON |
compute Depends: automatically with dpkg-shlibdeps (needs the dpkg-dev package) |
CPACK_DEBIAN_PACKAGE_SECTION, CPACK_DEBIAN_PACKAGE_HOMEPAGE |
Section:, Homepage: |
CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA |
maintainer scripts (postinst, prerm, …) |
CPACK_DEBIAN_FILE_NAME DEB-DEFAULT |
Debian-style file name name_version-release_arch.deb |
Building and inspecting a .deb
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"
HOMEPAGE_URL "https://example.com/tally" LANGUAGES CXX)
include(GNUInstallDirs)
add_executable(tally src/main.cpp)
install(TARGETS tally)
install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_DEBIAN_PACKAGE_RELEASE 1)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_PACKAGE_SECTION utils)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.34), libstdc++6 (>= 13)")
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
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cd build && cpack | grep package:
echo "--- control information"
dpkg-deb --field tally_0.3.1-1_amd64.deb
echo "--- files"
dpkg-deb --contents tally_0.3.1-1_amd64.deb | awk '{print $1, $2, $6}'
echo "--- unpack without installing, and run it"
dpkg-deb -x tally_0.3.1-1_amd64.deb ~/root
echo "one two three" | ~/root/usr/bin/tallyCPack: - package: /home/learner/tally/build/tally_0.3.1-1_amd64.deb generated.
--- control information
Architecture: amd64
Depends: libc6 (>= 2.34), libstdc++6 (>= 13)
Description: Counts words in text
Homepage: https://example.com/tally
Maintainer: Ada Lovelace <ada@example.com>
Package: tally
Priority: optional
Section: utils
Version: 0.3.1-1
Installed-Size: 37
--- files
drwxr-xr-x root/root ./usr/
drwxr-xr-x root/root ./usr/bin/
-rwxr-xr-x root/root ./usr/bin/tally
drwxr-xr-x root/root ./usr/share/
drwxr-xr-x root/root ./usr/share/doc/
drwxr-xr-x root/root ./usr/share/doc/tally/
-rw-r--r-- root/root ./usr/share/doc/tally/README.md
--- unpack without installing, and run it
3
Observations:
- The package installs under
/usr(CPACK_PACKAGING_INSTALL_PREFIXdefaults to/usrfor DEB), regardless ofCMAKE_INSTALL_PREFIX, and the files are owned byroot/root. Installed-Sizeand themd5sumsfile are computed by CPack.dpkg-deb -xextracts a package without installing it, the quickest way to check its contents. On a real system,sudo apt install ./tally_0.3.1-1_amd64.debinstalls it and resolvesDepends:.
Dependencies: by hand or computed
Writing Depends: by hand is fine for a small tool. For anything linking several system libraries, let dpkg-shlibdeps compute it from the binaries' NEEDED entries:
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)That needs dpkg-shlibdeps from the dpkg-dev package (installed in this course's image). This time the program uses std::cin/std::cout, so it links the C++ runtime as well as libc. readelf -d shows the NEEDED entries the tool starts from, and dpkg-deb --field prints the Depends: line it computed:
mkdir -p tally && cd tally
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(tally VERSION 0.3.1 DESCRIPTION "Counts words" LANGUAGES CXX)
add_executable(tally main.cpp)
install(TARGETS tally)
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
include(CPack)
CM
cat > main.cpp <<'SRC'
#include <iostream>
#include <string>
int main() {
std::string word;
int n = 0;
while (std::cin >> word) ++n;
std::cout << n << '\n';
}
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cd build && cpack 2>&1 | grep -E 'Error|Warning|shlibdeps|package:'
readelf -d tally | grep NEEDED
dpkg-deb --field tally_0.3.1_amd64.deb DependsCPack: - package: /home/learner/tally/build/tally_0.3.1_amd64.deb generated.
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
libc6 (>= 2.34), libgcc-s1 (>= 3.0), libstdc++6 (>= 13.1)
Nobody wrote that line by hand. dpkg-shlibdeps mapped each library to the Debian package that ships it (libstdc++.so.6 → libstdc++6), and took the minimum versions from the newest symbols the binary actually uses (here GCC 13's libstdc++ and glibc 2.34). Values you set in CPACK_DEBIAN_PACKAGE_DEPENDS are merged with the computed ones, which is how you add runtime dependencies that aren't linked libraries (a helper program, a data package).
Two caveats. The minimum versions describe the build machine: built on Ubuntu 24.04, the package demands libstdc++6 13.1, so it won't install on an older release; build on the oldest distribution you support. And if dpkg-shlibdeps is missing (no dpkg-dev on the packaging machine), CPack only warns (CPackDeb: Using only user-provided dependencies because dpkg-shlibdeps is not found.) and builds the package anyway with an empty Depends:. Such a package installs fine on the build machine and breaks elsewhere, so grep CI logs for CPackDeb: lines.
Versioning and upgrades
Version:must increase foraptto treat a package as an upgrade. Debian version comparison has its own rules:1.0~rc1sorts before1.0(the tilde), which is how pre-releases are expressed.- Bump
CPACK_DEBIAN_PACKAGE_RELEASE(the-1part) when you rebuild the same upstream version with packaging changes. - For publishing to an
aptrepository, sign the repository metadata (not the.deb) with tools likerepreprooraptly; that's covered in Release Engineering.
Test yourself
Where do files from install(TARGETS tally) end up inside a CPack DEB by default?
- /usr/local/bin
- /usr/bin
- /opt/tally/bin
- Wherever CMAKE_INSTALL_PREFIX points
What does CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON need?
- Network access
- The dpkg-shlibdeps tool from the dpkg-dev package
- A debian/ directory
- root privileges
How do you look inside a .deb without installing it?
apt showdpkg-deb --contents/dpkg-deb -xcpack --listtar -tzf