TExcelTAD

When CPM Is Enough

CPM.cmake asks for almost nothing: one CMake file, no new tool, no server. For many projects that is the right trade-off. For others, the lack of binaries and of real version resolution becomes painful. This lesson gives you a way to decide, and shows the cost CPM's simplicity hides.

The cost: everything compiles in your build

The same three dependencies the Mini Project uses, through CPM. Count how many source files the build compiles before reaching your own two:

bash · try it yourself
mkdir -p app && cd app
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
include(/opt/cpm/CPM.cmake)
CPMAddPackage("gh:fmtlib/fmt#12.1.0")
CPMAddPackage(NAME spdlog GITHUB_REPOSITORY gabime/spdlog VERSION 1.17.0 OPTIONS "SPDLOG_FMT_EXTERNAL ON")
CPMAddPackage("gh:catchorg/Catch2@3.16.0")

add_executable(app main.cpp)
target_link_libraries(app PRIVATE spdlog::spdlog)
add_executable(tests tests.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
enable_testing()
add_test(NAME tests COMMAND tests --rng-seed 1)
TXT
printf '#include <spdlog/spdlog.h>\nint main() { spdlog::set_pattern("%%v"); spdlog::info("hello"); }\n' > main.cpp
printf '#include <catch2/catch_test_macros.hpp>\nTEST_CASE("adds") { REQUIRE(1 + 1 == 2); }\n' > tests.cpp
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > build.log
echo "compiled $(grep -c 'Building CXX' build.log) source files:"
grep 'Building CXX' build.log | sed -E 's#.*object (_deps/)?([a-zA-Z0-9]+)[-/].*#\2#' | sort | uniq -c | sort -rn
ctest --test-dir build 2>&1 | grep -E 'tests passed'
compiled 118 source files:
    108 catch2
      6 spdlog
      2 fmt
      2 CMakeFiles
100% tests passed out of 1

The two CMakeFiles objects are your own main.cpp and tests.cpp; everything else is dependency code, nearly all of it Catch2. With Conan or vcpkg, those libraries arrive prebuilt, and your build compiles two files. A CI cache (ccache, or caching the build folder) recovers some of it, but every fresh checkout pays the price again.

A decision model

The choice comes down to a handful of questions. This model encodes the rules of thumb from the comparison lesson so you can try your own project's answers:

python · try it yourself
def choose(deps, heavy_deps, platforms, need_binaries, prebuilt_internal, team_size):
    reasons = []
    if heavy_deps:
        reasons.append("heavy dependencies (Boost, Qt, OpenSSL, LLVM) need prebuilt binaries")
    if need_binaries or prebuilt_internal:
        reasons.append("you ship or share prebuilt binaries")
    if len(platforms) > 2 and team_size > 5:
        reasons.append("many platforms and people: a binary cache pays off")
    if not reasons and deps <= 6:
        return "CPM.cmake", ["few, light, CMake-friendly dependencies"]
    if prebuilt_internal and team_size > 20:
        return "Conan (with a private remote)", reasons
    return "vcpkg or Conan", reasons

projects = {
    "CLI tool, 3 small deps":     (3, False, ["linux", "macos"], False, False, 2),
    "game, Qt editor + 15 deps":  (15, True, ["windows", "linux"], False, False, 8),
    "company SDK, 40 teams":      (25, True, ["windows", "linux", "macos", "android"], True, True, 120),
}
for name, answers in projects.items():
    tool, why = choose(*answers)
    print(f"{name:<28} -> {tool}")
    for w in why:
        print(f"{'':<31}- {w}")
CLI tool, 3 small deps       -> CPM.cmake
                               - few, light, CMake-friendly dependencies
game, Qt editor + 15 deps    -> vcpkg or Conan
                               - heavy dependencies (Boost, Qt, OpenSSL, LLVM) need prebuilt binaries
company SDK, 40 teams        -> Conan (with a private remote)
                               - heavy dependencies (Boost, Qt, OpenSSL, LLVM) need prebuilt binaries
                               - you ship or share prebuilt binaries
                               - many platforms and people: a binary cache pays off

CPM is enough when…

  • You have a few dependencies, all with good CMake support, and they compile quickly: fmt, spdlog, nlohmann_json, CLI11, doctest, GLM, and similar.
  • You're writing a library that others will consume: CPM with CPM_USE_LOCAL_PACKAGES lets your users bring their own packages from Conan, vcpkg or the system, without forcing a package manager on them.
  • You want zero setup for contributors: git clone && cmake -B build just works.
  • Examples, prototypes, coding exercises, game jams.

…and when it isn't

  • Heavy dependencies. Boost, Qt, OpenSSL, ICU, LLVM, gRPC: compiling them in every build tree is minutes to hours, and many don't build cleanly via add_subdirectory.
  • Non-CMake projects. Autotools or Meson libraries need custom glue; Conan recipes and vcpkg ports already contain it.
  • Version conflicts. CPM uses the first declaration of a package name it sees. If two dependencies want different fmt versions, the first wins silently (CPM prints a warning if a later request asks for a newer version). There's no solver.
  • Binary distribution, multiple configurations per CI job, air-gapped builds without a source mirror, or license/SBOM requirements that need a package inventory.
  • Target name collisions: everything lives in one CMake project, so two dependencies that both define a utils target or set global variables will clash.

A middle path

Many teams start with CPM and move to vcpkg or Conan as the project grows. Make that move cheap from the start:

  1. Always link the namespaced targets the real packages export (fmt::fmt, spdlog::spdlog), never internal names like fmt without the namespace.
  2. Turn on CPM_USE_LOCAL_PACKAGES so a package manager can take over without editing CMakeLists.txt.
  3. Keep the CPMAddPackage calls in one file (cmake/dependencies.cmake) so they're easy to replace with find_package.

Test yourself

In the CPM build, where did nearly all the compile time go?

  1. The app's main.cpp
  2. The dependencies' sources, mainly Catch2
  3. Downloading
  4. Running ctest

Two dependencies ask CPM for different fmt versions. What happens?

  1. CPM picks the newest
  2. The first declaration wins; a later request for a newer version only triggers a warning
  3. Both are built
  4. A hard error

Which project is the best fit for CPM.cmake alone?

  1. A Qt + Boost desktop app on 4 platforms
  2. A small CLI with fmt and CLI11
  3. A company SDK with prebuilt binaries for customers
  4. A project depending on an Autotools-only library