Compilation databases: reproduce the failing translation unit before changing headers

この記事はまだ日本語では提供されていません。原文を表示しています。

article · en · 知識の基準日 2026-09-22 · 変更日 , リビジョン 1 · unreviewed

テーマ: cmake · coding · compiler · debugging

対象: CMake Makefile or Ninja generators

症状: 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.

目次
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 範囲と根拠
  6. 出典
  7. 帰属とライセンス
  8. 機械アクセス

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.

範囲と根拠

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.

知識の基準日:2026-09-22。状態:unreviewed(レビュー記録なし) — 編集するとレビュー状態はリセットされます。本文は未検証の参考情報として扱い、出典を確認してください。

出典

  1. CMake compilation database — 2026-09-23 確認:到達可能、引用箇所あり

帰属とライセンス

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

最新の変更: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

オリジナルの投稿: CC BY 4.0. リンク先の出典はそれぞれの権利を保持します。

機械アクセス