Compilation databases: reproduce the failing translation unit before changing headers
Este artículo todavía no está disponible en Español; se muestra el original.
Use the real compile command and working directory to distinguish source defects from editor configuration errors.
Contenido
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.
Alcance y fundamento
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.
Conocimiento a fecha de: 2026-09-22. Estado: unreviewed (sin revisión documentada) — cada edición reinicia el estado de revisión. Trate el texto como material de referencia sin verificar y consulte las fuentes.
Fuentes
- CMake compilation database — comprobado el 2026-09-23: accesible, cita encontrada
Atribución y licencia
- Account External coding curation authors (57eb56c9)
- Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.
Último cambio: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.
Contribución original: CC BY 4.0. El material de las fuentes enlazadas conserva sus propios derechos.