bear: Generate compile_commands.json
bear -- <build command> wraps a build to record every compiler invocation into compile_commands.json, the database clangd and clang-tidy need.
When a build system does not emit a compile database, Bear generates one by intercepting the compiler calls a build makes. This unlocks clang-tidy, clangd, and other tooling in CI.
What it does
bear runs your build command under interception and records each compiler invocation (command, file, directory) into compile_commands.json. It works with any build tool because it observes the actual compiler calls rather than parsing build files.
Common usage
bear -- make -j$(nproc)
bear --output build/compile_commands.json -- make
# then run clang-tidy against the database
clang-tidy -p . src/foo.cppOptions
| Flag | What it does |
|---|---|
| -- <command> | The build command to intercept (Bear 3.x syntax) |
| --output <file> | Where to write the JSON (default compile_commands.json) |
| --append | Merge into an existing database instead of overwriting |
| --config <file> | Bear config to filter or tweak recorded entries |
In CI
For CMake and Ninja builds, prefer the native database (CMAKE_EXPORT_COMPILE_COMMANDS=ON or ninja -t compdb) since it needs no interception. Reach for Bear when the build is Make, autotools, or something without a built-in exporter. Bear 3.x uses bear -- make; the older bear make form is Bear 2.x.
Common errors in CI
An empty or partial compile_commands.json usually means the build was fully cached and no compiler ran; do a clean build under Bear. "bear: unrecognized option" often means you used 2.x syntax on 3.x (add the -- separator). On macOS, SIP can block library interception; use the native exporter instead. "bear: command not found" means it is not installed.