← C++

C++ · 15 / 24

Reproducible desktop builds and packages

Build and test separate configurations and verify an installable package.

  • Large
  • Advanced
  • 90 min

Prerequisites: Headers, CMake and first unit tests, Your first Qt interface with signals and slots, Background work, performance and desktop packages

In this tutorial
  1. Define the contract
  2. Separate tools and build
  3. Check observable expectations
  4. Detect an incorrect change
  5. Run the exercise separately
  6. Interpret the verification boundary
  7. Exercise and solution
  8. Project context and sources

Download learning package

Setup and verification

GCC with C++20 and CMake 3.25 or newer. Python 3 is also required for the common verification entry point. Extract the learning package into a new folder and run python3 run_checks.py there. The script copies the included files into a temporary learning directory. It verifies the example and then the supplied solution there, preserving the original files. Required packages are downloaded on the first run.

Verified on 2026-10-21 on Linux x86_64. The Linux check builds, tests and packages the small program. The Windows matrix is prepared. A successful run must be independently demonstrated on a real Windows runner.

A build directory stores compiler, generator and configuration state. Separate directories prevent shipping an old debug build as release. CTest verifies behaviour, install defines delivery layout and CPack creates an archive. Reproducible here means documented inputs and a repeatable workflow, not claimed byte identity across compilers.

Package contents and individual files

01Define the contract

CMake describes targets, dependencies and installation. CPack creates a package from that description. The target platform determines runtime libraries and package format. A Linux build does not validate a Windows binary.

main.cpp · cppOpen file
#include <iostream>
#include <string>
int main(int argc, char **argv) {
  if (argc == 2 && std::string(argv[1]) == "--self-test") {
    std::cout << "self-test passed\n";
    return 0;
  }
  std::cout << "Factory learning 1.0\n";
}

Run / check

python3 verify.py

02Separate tools and build

Put all files in a new folder. Read the compiler options and run verify.py. A failed command must propagate its exit code to the caller.

verify.py · pyOpen file
import subprocess

subprocess.run(["cmake", "-S", ".", "-B", "build"], check=True)
subprocess.run(["cmake", "--build", "build", "--config", "Release"], check=True)
subprocess.run(
    ["ctest", "--test-dir", "build", "-C", "Release", "--output-on-failure"], check=True
)
subprocess.run(
    ["cmake", "--install", "build", "--prefix", "delivery", "--config", "Release"],
    check=True,
)
subprocess.run(
    ["cpack", "--config", "build/CPackConfig.cmake", "-C", "Release"], check=True
)
from pathlib import Path
import os

name = "lesson.exe" if os.name == "nt" else "lesson"
subprocess.run([str(Path("delivery/bin", name).resolve()), "--self-test"], check=True)

03Check observable expectations

The Linux check builds, tests and packages the small program. The Windows matrix is prepared. A successful run must be independently demonstrated on a real Windows runner.

CMakeLists.txt · cmakeOpen file
cmake_minimum_required(VERSION 3.20)
project(learning LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(lesson main.cpp)
enable_testing()
add_test(NAME behaviour COMMAND lesson)
install(TARGETS lesson RUNTIME DESTINATION bin)
set(CPACK_PACKAGE_NAME factory-learning)
set(CPACK_PACKAGE_VERSION 1.0.0)
set(CPACK_GENERATOR ZIP)
include(CPack)
Expected result

The Linux check builds, tests and packages the small program. The Windows matrix is prepared. A successful run must be independently demonstrated on a real Windows runner.

04Detect an incorrect change

Change a checked condition or expected result. Verification must now fail. Restore the original version afterwards. A green process alone does not explain which property is protected.

05Run the exercise separately

Create a debug package and run the same self-test from its installation directory.

06Interpret the verification boundary

Debug and release use separate directories. On Windows use the appropriate Visual Studio terminal. A Linux check replaces neither a native Windows run nor Qt runtime verification of the complete FactoryForge app.

Expected result

Verification finishes successfully and checks the conditions named in the example.

Your exercise

Create a debug package and run the same self-test from its installation directory.

Show the solution and explanation

Debug and release use separate directories. On Windows use the appropriate Visual Studio terminal. A Linux check replaces neither a native Windows run nor Qt runtime verification of the complete FactoryForge app.

solution.py · pyOpen file
import subprocess

subprocess.run(
    ["cmake", "-S", ".", "-B", "build-debug", "-DCMAKE_BUILD_TYPE=Debug"], check=True
)
subprocess.run(["cmake", "--build", "build-debug", "--config", "Debug"], check=True)
subprocess.run(
    ["ctest", "--test-dir", "build-debug", "-C", "Debug", "--output-on-failure"],
    check=True,
)
subprocess.run(
    [
        "cmake",
        "--install",
        "build-debug",
        "--prefix",
        "delivery-debug",
        "--config",
        "Debug",
    ],
    check=True,
)
from pathlib import Path

name = "lesson.exe" if __import__("os").name == "nt" else "lesson"
subprocess.run(
    [str(Path("delivery-debug/bin", name).resolve()), "--self-test"], check=True
)

Project context and sources

This independent learning project explores a topic in this technology area. Project references provide a pinned revision as architectural context. They do not imply that every library or feature introduced here is already used in that application.