Skip to navigation

CMake find_package vs FetchContent: Choose a Dependency Boundary

In short: Find when CMake should consume an installed package, include dependency sources, or use a provider.

Published 20 min read
On this page

Direct answer

CMake gives a project three ways to satisfy a dependency: consume an installed package with find_package, include the dependency’s sources with FetchContent, or intercept the request with a dependency provider. The choice is a boundary decision about who owns the dependency build and how overrides behave, and it should be made before comparing command syntax. Providers intercept first. FetchContent can try find_package before building dependency sources unless it is overridden. The concrete win is a documented boundary that says whether a library is an external package or part of the build. The concrete loss is that neither path is universally more reproducible or more stable, and the override order means the boundary can be redirected by a provider or option the review never saw. The practical implication is that a dependency decision needs the override path written down next to the declaration. Ask Claude Code to trace which path a given dependency actually took before proposing a change.

Why the wrong boundary costs a build

Two projects can declare the same library and get two different builds out of it. One consumes an installed package and links whatever version the environment provides. The other downloads the sources and builds them as part of the project. Neither is wrong, and mixing them without a record is where the time goes.

The failure surfaces as a version that differs between two machines, a build that reaches the network unexpectedly, or a dependency that cannot be overridden because the override path was never named. An agent asked to “add the library” will pick a path silently, and the boundary it chose becomes a constraint the team discovers later.

Evidence and its limits

Official, F039: The CMake using dependencies guide documents provider interception before built-in dependency methods, and records that FetchContent can try find_package before building dependency sources unless overridden. The exact path depends on declared arguments, provider setup, and override options.

Official: The FetchContent module documents the declaration, population, and override options for including dependency sources.

Official: The find_package command documents the search modes and the config and find module distinction.

Official: The dependency provider documentation documents cmake_language(DECLARE_DEPENDENCY_PROVIDER) and provider interception.

Official, F040: CMake GitLab issue #22619 is an upstream discussion about an API for dependency providers for find_package and FetchContent, surfaced by search with a 2021 date. No current defect, fix, or universal behavior is inferred from it.

Official, agent loop: Claude Code best practices recommends pairing an edit with a pass-or-fail check. For this decision that check is a trace of which dependency path was taken.

No claim here depends on a CMake version, a specific library, or a benchmark.

Three dependency paths

The three paths differ in who builds the dependency and who can override the choice. Read them as boundaries rather than as syntax alternatives.

Path Who builds the dependency Version source Override path
find_package The environment, already installed Installed package Provider, or CMAKE_PREFIX_PATH
FetchContent The project, from sources Declared Git tag or archive FETCHCONTENT_SOURCE_DIR_<name> or try-find-package
Dependency provider Delegated by the provider Provider decides The provider itself

Treat the table as a decision frame. The question is not which command is shorter but which boundary the project wants, and whether that boundary can be redirected without notice.

Read find_package requests

find_package asks CMake to locate an already-installed package and load its configuration. It does not build anything. Two search modes exist, and the config mode is the one most modern packages support.

Concern Config mode Module mode Review question
Provided by The package itself A Find<Name>.cmake file Who authored the find logic?
Version handling Package’s own config Module’s logic Whose version rule applies?
Target names Package-defined Module-defined Which targets does the consumer expect?

Examine the find request for three things: which mode applies, which targets the consumer expects, and where the package is expected to be found. A find_package that succeeds is a find_package that found something, and what it found is decided by the environment as much as by the command.

Inspect FetchContent declarations

FetchContent brings the dependency’s sources into the build. A declaration names the source, and population makes it available. The module documents both, and the review is about what the declaration fixes and what it leaves open.

Declaration What it pins What it leaves open
GIT_REPOSITORY plus GIT_TAG Source and revision Build options the dependency exposes
URL plus URL_HASH Archive and integrity Same, build options
FIND_PACKAGE_ARGS Whether try-find-package runs Which installed package is found

The try-find-package behavior in F039 is the part that surprises reviewers. A FetchContent declaration can attempt find_package first, so a library the team expected to build from sources may instead be consumed from an installed copy. The FIND_PACKAGE_ARGS and the override options decide this, and the override path belongs in the review.

Understand provider interception

A dependency provider is a CMake script that intercepts dependency requests before the built-in methods run. F039 records that providers intercept first. A provider can redirect a request to a specific installed package, to a source tree, or to a mirror.

Provider behavior Effect on the boundary Review action
Redirects to an installed package Overrides a source-inclusion intent Confirm the redirect is intended
Redirects to a source tree Overrides an installed-package intent Confirm the tree is the right one
Passes through Built-in methods decide Record that no interception occurred

Providers are powerful and invisible in the project file that declares the dependency. Search for DECLARE_DEPENDENCY_PROVIDER in the build tree and the tooling before concluding which path a dependency took. A provider in a toolchain or a preset-loaded script can redirect everything.

Trace override order

The override order is the part of the review that prevents the most rework. Providers first, then the FetchContent try-find-package behavior, then the built-in method.

Order Mechanism What it overrides
1 Dependency provider Everything before built-in methods
2 FetchContent try find_package Source inclusion, unless overridden
3 FetchContent builds sources The fallback
4 find_package directly None, no FetchContent involved

Trace the order for the specific dependency under review rather than assuming it. The exact path depends on declared arguments, provider setup, and override options, which is the caveat F039 carries. A trace that shows a path the team did not intend is the concrete finding.

bash
cmake --fresh --preset <name>

A fresh configure re-runs the resolution without a stale cache, which is the cleanest way to observe which path a dependency takes. Compare the output before and after any proposed change.

Compare config and find modules

When find_package runs, the distinction between config and find modules matters for review because it decides whose version and target logic applies.

Aspect Config package Find module
Author The package’s maintainers The consumer or CMake
Target names Package namespace Module-defined
Version selection Package’s own Module’s logic
Stability across versions Package-controlled Consumer-controlled

A consumer that expects targets from a config package can break when it is redirected to a find module, and the reverse. Record which mode the dependency is expected to use, because the override path can change it.

Document the chosen boundary

A dependency boundary is a contract. Record four things next to the declaration: the chosen path, the override path, the expected targets, and who owns the build.

Question Where the answer lives Pass condition
Which path was chosen Declaration plus trace output Named with evidence
What can override it Provider search plus options Providers found or none
Which targets are expected Consumer CMake Targets exist after resolution
Who builds the dependency Boundary decision Stated as environment or project

Ask Claude Code to produce the trace before proposing a path change. The CMake presets guide covers recording the configure inputs that determine which of these paths can even run.

A worked walkthrough of one boundary decision

Consider a project that declares a dependency with FetchContent, expecting the sources to be built as part of the project. A dependency provider is registered in a toolchain script the team forgot about. Configure runs, the provider intercepts the request, and an installed copy of the library is consumed instead. The build succeeds and the sources are never fetched.

The instinctive reading is that FetchContent is broken. The actual cause is interception, which F039 records as happening before built-in dependency methods. The review question is which path actually ran, and the answer requires a trace rather than a reading of the project file.

Search the build tree and tooling for DECLARE_DEPENDENCY_PROVIDER. This is the step teams skip and later regret, because a provider can redirect every dependency request invisibly.

bash
cmake --fresh --preset <name>

Run a fresh configure to re-resolve without a stale cache. Read the output for the dependency under review and note whether it was found, fetched, or intercepted. The trace is the evidence the boundary decision rests on.

Record four things in the change description: the path that ran, the providers found or the absence of them, the override path for the chosen boundary, and who builds the dependency. A reviewer reading those four can tell whether the library is an external package or part of the build, and what would redirect it.

Anti-patterns that look like boundary decisions

Four patterns recur around dependency boundaries, and each one hides a decision a reviewer needs to see.

Pattern What it looks like Why it hurts Do instead
Choosing on ideology Standardizing on one path everywhere Neither path fits every dependency Decide per dependency
Skipping the provider search Reading only the project file A provider can redirect everything Search for DECLARE_DEPENDENCY_PROVIDER
Ignoring the override path Naming only the command The override decides what actually happens Record the override path
Claiming universal reproducibility “FetchContent is always reproducible” Neither path is universally better Keep the claim per dependency

Claiming universal reproducibility deserves the most caution. FetchContent pins a revision, which is one form of control, but the toolchain and build options still vary, and a try-find-package can consume an installed copy. The guardrails in this post’s avoid list forbid claiming either path is universally better, and that limit is the honest position.

None of these is a CMake defect. They are review hazards, and the provider search plus the resolution trace are what catch them.

A runbook for the next dependency decision

The checklist earlier in this post is the pass condition. The runbook below is the sequence to follow when a dependency boundary question arises.

Decide the boundary in principle: is this library an external package the environment provides, or part of the build the project compiles? Write the answer down before comparing command syntax.

Search for dependency providers in the build tree, toolchain, and tooling. Record what you found or that you found none.

Trace the dependency with a fresh configure and record which path actually ran. Compare it against the boundary you decided, and investigate any mismatch.

Name the override path for the chosen boundary. The override is part of the decision, not an implementation detail.

Record the expected targets and which mode, config or find module, applies. Verify the targets exist after resolution.

The runbook takes minutes and produces the artifacts a reviewer needs: the trace, the provider result, and the override path. Skipping it produces a declaration that looks decided and is silently redirected.

Where this habit pays across the batch

The trace-first habit in this post is the same one the rest of the batch applies to different constructs. CMake presets asks for the resolved preset inputs before a configure, and Conan CMakeDeps asks for the target contract before a layout change.

The JVM posts apply the same discipline to version and variant policy. Gradle constraints vs dependencies asks for a resolved-graph diff, Gradle capability conflicts asks for a component inventory, Gradle variant attributes asks for the consumer request, and Maven optional dependencies asks for an import scan.

The .NET posts apply it to package versions and generated code. NuGet central package management asks for a props and override inventory, .NET multi-targeting asks for a per-framework matrix and build, and C# source generators asks for the generator’s input change before reading its output.

That consistency is deliberate. The constructs differ, but the shape is the same: read the evidence the tool already produces, then make the smallest change the evidence supports.

The same shape extends to the ecosystems covered in earlier batches. On the Python side, dependency groups versus extras separates local workflow requirements from published optional features, and stub drift audits a package’s declared types against its documented runtime surface. TypedDict boundaries covers separating truly optional keys from missing data before replacing broad dictionary annotations.

On the Go side, module compatibility covers keeping an exported API change inside an import contract, and graph pruning covers why an indirect dependency entry is graph metadata rather than something to delete. Workspace boundaries covers deciding what belongs in go.work versus what stays a module requirement.

On the Rust side, feature unification covers mapping which optional APIs a workspace build actually exposes, and workspace inheritance covers centralizing metadata without hiding crate-local exceptions. Profile boundaries covers separating diagnostic and optimization settings from options that change observable program behavior.

Choosing a dependency boundary has a direct analog in Go workspace boundaries, where the same “what belongs inside the build versus what stays external” question applies to workspace membership rather than CMake dependency paths. Both are boundary questions, and both reward tracing what actually happened before deciding what should.

Reading a fresh configure as evidence

A fresh configure is the cleanest way to observe which dependency path actually ran, and it is worth reading as evidence rather than as a green or red line.

The dependency resolution output names each package and how it was satisfied. Find the line for the dependency under review and classify it: found by find_package, fetched by FetchContent, or intercepted by a provider. That classification is the trace, and it is the artifact the boundary decision rests on.

Absence of a fetch line is itself evidence. If you expected sources to be downloaded and no fetch appears, the request was satisfied another way, most likely by a provider or a try-find-package. That is the mismatch between intent and reality the review exists to catch.

Configure output What it confirms What it does not
Found-package line An installed copy was used That it is the version you intended
Fetch line Sources were downloaded and populated That the build options are what you expect
Provider log line Interception occurred That the redirect matches intent

Compare the fresh configure against the previous one after any proposed change. A path that changed without an intended decision is the finding, and it is the concrete evidence a reviewer needs rather than a description of what the CMake says.

Working alongside the review bottleneck

A boundary decision with a pasted trace and provider search is cheap to review. A decision described as “switched to FetchContent” is a reviewer reproducing the work, and reproduction is the expensive part. The Claude Code code review bottleneck covers what happens when generation outpaces the review that has to accept it.

The same reasoning appears in Claude Code cost per verified change, which treats verified work as a separate unit from generated output. A resolution trace is a verified unit. A “trust me, it uses the sources now” description is not.

When the review queue is long enough that verification slips, the failure mode is a merge that nobody actually checked. Prevent false success from passing tests covers the case where a successful configure is the only evidence, and a redirected dependency is exactly the case where green proves less than it appears to.

The concrete win, loss, and implication

The concrete win is a dependency boundary that is written down and traceable. A reviewer reading the declaration, the trace, and the override path can tell whether the library is an external package or part of the build, and what would redirect it.

The concrete loss is the absence of a universally better option. Neither find_package nor FetchContent is always more reproducible or always more stable. A project that standardizes on one path for ideological reasons pays for it on the projects where the other fit. The honest position is per-dependency, with the override path named.

The practical implication is that the override path is part of the dependency, not an implementation detail. Teams that record it resolve fewer “why did this library come from there” questions. Teams that name only the command spend the difference tracing providers and options after the fact.

Dependency boundaries continue across the batch. Conan CMakeDeps covers what Conan generates and how layout changes affect consumer discovery. CMake presets covers the configure inputs that determine the environment these paths run in.

For other ecosystems, Maven optional dependencies covers which optional libraries a consumer must declare directly, and Gradle capability conflicts covers two components claiming one capability.

For .NET, NuGet central package management covers which central version wins, and .NET multi-targeting covers per-framework package versions.

Supply-chain and provenance questions are adjacent but out of scope here. The Claude Code hallucinated packages guide covers blocking a nonexistent dependency before it reaches the build.

Evidence map showing a dependency provider intercepting first, then FetchContent trying find_package before building sources, then find_package consuming an installed package.
Editorial evidence map based on F039 and F040. CMake’s guide says providers intercept first; the exact path depends on arguments and override options.

Checklist before choosing a dependency path

Run this list against the decision. Each row is a pass condition.

Check Pass condition If it fails
Chosen path Named with trace evidence Run a fresh configure and trace
Provider search Providers found or none Search for DECLARE_DEPENDENCY_PROVIDER
Override path Named in the record Name it before merging
Expected targets Exist after resolution Verify target names
Build ownership Stated as environment or project Decide and record
Mode expected Config or find module named Confirm which mode applies
Universality claim Not made Keep the decision per dependency
Workflow from reading find_package and FetchContent declarations, through a provider search and override trace, to a documented boundary with expected targets.
Editorial workflow derived from F039 and F040. The sequence describes a boundary review; it does not claim either path is universally better.

Working with Claude Code on a dependency boundary

Give the agent the library name and the CMake files that reference it. Ask for a fresh-configure trace of which path the dependency took, plus a search for dependency providers, before proposing a change. The agent session works best when the check is a trace that names the resolved path, which is the shape the official best-practices guidance recommends.

Ask the agent to report five things when it finishes. The path the dependency took. The providers found or the absence of them. The override path for the chosen boundary. The expected targets and whether they resolved. Which mode, config or find module, applies. A report that omits the provider search has not finished, because a provider can redirect the whole boundary invisibly.

Reviewing the boundary choice is the reviewer’s job. The agent reads CMake and can trace resolution, but deciding whether a library should be an external package or part of the build is a design decision. The Claude Code verification guide covers the review questions: what changed, what proves it, and what was not checked.

Tradeoff between consuming an installed package for a lighter build and including sources for a self-contained build, with the override path deciding which actually happens.
Editorial tradeoff diagram based on F039 and F040. Neither path is universally better, and the override path is part of the decision.

Data gap and future update

F039 carries the caveat that the exact path depends on declared arguments, provider setup, and override options, and F040 is a dated upstream discussion used only as labeled community context. This post does not claim either path is universally reproducible or stable. A future update should record, per project, which dependencies use each path, which providers are registered, and the CMake version the observations were taken at.

No benchmark, build-time measurement, or source-provenance claim is made here. Whether one path produces a more secure build is out of scope and would require separate evidence.

One AI API Key option for OpenAI-format agents

Claude Code uses an Anthropic-format route, so the One AI free model IDs are not a Claude Code model source. The packet records Claude IDs as unavailable on 2026-11-27.

OpenAI-format agents have a separate route: One AI API Key offers https://api.oneaiapikey.com/v1, one key, and 46 live free IDs. It is a different client and request format, not a workaround for Claude Code authentication. Check the free model list and eligibility notes before choosing an agent route, or read the quickstart for the request shape.

FAQ

Is FetchContent always more reproducible?

No. It builds the dependency’s sources at a pinned revision, which is one form of control, but the installed-toolchain and build options still vary, and a try-find-package can consume an installed copy instead. Neither path is universally more reproducible.

Is find_package always more stable?

No. It depends on what the environment has installed, which varies by machine. A find_package that succeeds on one machine can fail or find a different version on another. Naming the override path beats assuming stability.

Can a provider change my find_package call?

Yes. F039 records that providers intercept before built-in dependency methods, so a registered provider can redirect a find_package request before the command’s own logic runs. Search for DECLARE_DEPENDENCY_PROVIDER before concluding which path ran.

Why did FetchContent use an installed package?

Because a FetchContent declaration can try find_package before building dependency sources unless it is overridden, as F039 records. The FIND_PACKAGE_ARGS and the override options decide this. Trace it with a fresh configure.

What is issue #22619 about?

It is an upstream CMake GitLab discussion about an API for dependency providers for find_package and FetchContent, used here as labeled community context with a 2021 date. No current defect, fix, or universal behavior is inferred from it.

This post is one of ten build-system and dependency guides written together. CMake Presets and Conan CMakeDeps complete the C++ set.

The JVM posts cover dependency and variant boundaries: Gradle Constraints vs Dependencies, Gradle Capability Conflicts, Gradle Variant Attributes, and Maven Optional Dependencies.

The .NET posts cover version and generated-code boundaries: NuGet Central Package Management, .NET Multi-Targeting, and C# Source Generators.

More from this batch

More Claude Code troubleshooting

Sources

Control checklist covering chosen path, provider search, override path, expected targets, build ownership, and mode expected.
Editorial control checklist derived from F039 and F040. Passing it shows the boundary was traced; it does not make either path universally better.
Verdict that a traced, documented dependency boundary with a named override path is reviewable, while an untraced declaration can be redirected by a provider.
Editorial verdict based on F039 and F040. The boundary is a decision with an override path, not a command choice.
More in Blog

Try it

One API key for GPT, Grok, DeepSeek, Kimi, GLM, MiniMax, Qwen, Step, Beam, Nemotron, ERNIE, Hunyuan, Gemma, Laguna, LFM, LongCat, Llama, Solar, Jais, Mistral, K2 and Roleplay.

One AI API Key speaks the OpenAI and Anthropic formats, so you change the base URL and the key, not your code. Credit packs start at $10, and every model has a published per-token rate.

New accounts$10 credit on sign-up
Claim $10 credit →