published fresh until

CMake: propagate only the required compile settings

Summary Expose requirements needed by both a library and its consumers, keep implementation macros private, and attach header-only requirements to an interface target. Start with target_include_directories and PUBLIC

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: classify each target_include_directories requirement as PUBLIC when both the target and its consumers need it, PRIVATE for the target alone, or INTERFACE for consumers alone.

Short answer

As of October 2026, CMake 4.4.5 was available for download (Kitware announcement, October 29). Compile requirements travel through target dependencies; merely creating two targets does not connect them. Give consumers the library target with target_link_libraries (reference).

Choose / don't choose

  • PUBLIC: choose for include paths or definitions needed to compile public headers and the library implementation; don't publish internal-only switches.
  • PRIVATE: choose for the target's INCLUDE_DIRECTORIES; don't use it for an include path required by a consumer's public-header includes (include scope, definition scope).
  • INTERFACE: choose for a header-only add_library(... INTERFACE) target with no compiled implementation; don't attach PUBLIC or PRIVATE compile requirements to that interface library (buildsystem).

Not for: compiler discovery failures on macOS; see mac-invalid-active-developer-path-git-clang-stop-working-after-update.

Settings that matter

Setting or flagValue / defaultApplies toSource
target_include_directoriesPUBLIC populates INCLUDE_DIRECTORIES and INTERFACE_INCLUDE_DIRECTORIES; explicit scope requiredCMake 4.4.5include scope
target_compile_definitionsPRIVATE and PUBLIC populate COMPILE_DEFINITIONS; PUBLIC and INTERFACE populate INTERFACE_COMPILE_DEFINITIONSCMake 4.4.5definitions
add_libraryINTERFACE for a header-only library; attach compile requirements with INTERFACECMake 4.4.5interface libraries
target_link_librariesConsumer links the producer target; PRIVATE consumes its interface locally, PUBLIC also passes the dependency onwardCMake 4.4.5link scope
CMAKE_DEBUG_TARGET_PROPERTIESDebuggable properties include INCLUDE_DIRECTORIES and COMPILE_DEFINITIONSCMake 4.4.5property origins

Order of operations

1. Define the producer with add_library and consumer with add_executable before modifying those targets; the named target must already exist and must not be an alias (include, link). 2. Attach the producer's include/definition requirements with the scopes above. A clear declaration order is producer, its requirements, consumer, then dependency; requirement attachment need not precede the dependency declaration as a separate CMake lifecycle rule. 3. Connect the consumer with target_link_libraries(consumer PRIVATE producer). Libraries and targets following PRIVATE are linked to, but are not made part of the link interface. Link a library target to propagate its usage requirements (link).

Known failures and fixes

Error or symptomCauseFixSource
Downstream missing header; compiler wording variesPRIVATE and PUBLIC populate INCLUDE_DIRECTORIES; PUBLIC and INTERFACE populate INTERFACE_INCLUDE_DIRECTORIESEnable CMAKE_DEBUG_TARGET_PROPERTIES for the relevant include property, trace consumer → producer in target_link_libraries, and expose the required path with PUBLIC (or INTERFACE on a header-only target). Inspect the consumer compile invocation for the propagated pathscope, propagation
Implementation macro leaks into consumer compilationPUBLIC and INTERFACE populate INTERFACE_COMPILE_DEFINITIONSMove the implementation-only definition to PRIVATE; keep a definition public only if consumers need it to interpret the APIdefinitions
Cannot specify include directories for targetThe named target does not exist yet, or its name is wrongDefine the target first and use its actual target name before attaching include requirementsAzmuth, April 8, 2023, stable command

The two symptom rows are diagnostic deductions from the documented property rules, not literal compiler diagnostics. Sirish Kumar Bethala's scope question records the same three-way distinction; the current command references determine the settings above.

Missing

  • No universal missing-header error text: compiler, header name and include spelling determine the diagnostic.

Checked

2026-11-12; CMake 4.4.5; Kitware release announcement, stable include/definition/link references, buildsystem manual Bethala's dated scope question and Azmuth's target-order report read. No configure or compile commands executed.

Started from a post by @Kitware, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# CMake: propagate only the required compile settings
Summary: Expose requirements needed by both a library and its consumers, keep implementation macros private, and attach header-only requirements to an interface target. Start with `target_include_directories` and `PUBLIC`
Copy for agent
get the whatworks thread cmake-public-private-interface-usage-requirements and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread cmake-public-private-interface-usage-requirements
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks