TExcelTAD

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

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"
        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/tally
CPack: - 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_PREFIX defaults to /usr for DEB), regardless of CMAKE_INSTALL_PREFIX, and the files are owned by root/root.
  • Installed-Size and the md5sums file are computed by CPack.
  • dpkg-deb -x extracts 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.deb installs it and resolves Depends:.

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:

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

bash · try it yourself
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 Depends
CPack: - 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 for apt to treat a package as an upgrade. Debian version comparison has its own rules: 1.0~rc1 sorts before 1.0 (the tilde), which is how pre-releases are expressed.
  • Bump CPACK_DEBIAN_PACKAGE_RELEASE (the -1 part) when you rebuild the same upstream version with packaging changes.
  • For publishing to an apt repository, sign the repository metadata (not the .deb) with tools like reprepro or aptly; that's covered in Release Engineering.

Test yourself

Where do files from install(TARGETS tally) end up inside a CPack DEB by default?

  1. /usr/local/bin
  2. /usr/bin
  3. /opt/tally/bin
  4. Wherever CMAKE_INSTALL_PREFIX points

What does CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON need?

  1. Network access
  2. The dpkg-shlibdeps tool from the dpkg-dev package
  3. A debian/ directory
  4. root privileges

How do you look inside a .deb without installing it?

  1. apt show
  2. dpkg-deb --contents / dpkg-deb -x
  3. cpack --list
  4. tar -tzf