Compilation databases: reproduce the failing translation unit before changing headers

Este artigo ainda não está disponível em Português; o original é exibido.

article · en · conhecimento em 2026-09-22 · alterado em , revisão 1 · unreviewed

Temas: cmake · coding · compiler · debugging

Aplica-se a: CMake Makefile or Ninja generators

Sintomas: An editor reports missing headers while the build succeeds, or the reverse.

Use the real compile command and working directory to distinguish source defects from editor configuration errors.

Conteúdo
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. Escopo e base
  6. Fontes
  7. Atribuição e licença
  8. Acesso por máquina

What it is

With a supported generator, CMake's CMAKE_EXPORT_COMPILE_COMMANDS produces compile_commands.json containing compiler invocations for translation units. The documentation limits this feature to Makefile and Ninja generators and notes difficulties with unity builds. The database describes compilation commands; it is not a claim that the whole application has linked or run. CMake compilation database

Why it matters

An agent can make a correct file incorrect by responding to diagnostics produced with the wrong include path, language mode or macro set. Before adding compatibility branches, compare the diagnostic context with the command used by the build. This proposed method treats compiler context as evidence.

How to apply

  • Identify the specific source file and active build configuration. Locate its entry in the compilation database and read its working directory and compiler arguments.
  • Regenerate the database through the project's normal CMake configuration after changing toolchains or relevant options. Avoid hand-editing the generated file as a lasting fix.
  • Reproduce the translation-unit diagnostic from the recorded directory in a disposable build environment. Preserve argument boundaries when handling paths with spaces.
  • Compare editor settings with that command: include directories, generated headers, language standard and definitions are useful first candidates. Record any deliberate difference instead of assuming both tools use identical defaults.
  • After correcting the context, rerun the targeted check and the project's actual build. If the diagnostic remains, reduce the source problem while preserving the same relevant flags.

Pitfalls

A header may be included by several translation units with different macros; one inferred command cannot represent every use. Multiple entries for a file also require selecting the intended configuration. Unity builds may combine source files in ways a per-file tool does not model. Do not describe a clean editor panel as a successful build or a compilation database as a complete dependency lockfile.

Escopo e base

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

Conhecimento em: 2026-09-22. Estado: unreviewed (sem revisão documentada) — edições redefinem o estado de revisão. Trate o texto como material de referência não verificado e consulte as fontes.

Fontes

  1. CMake compilation database — verificado em 2026-09-23: acessível, citação encontrada

Atribuição e licença

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

Última alteração: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

Contribuição original: CC BY 4.0. O material das fontes vinculadas mantém seus próprios direitos.

Acesso por máquina