Generate compile_commands.json for any C or C++ build
Bear gives you working code navigation, autocomplete, and diagnostics on
any C or C++ project, whatever the build system. Prefix your build
command with bear -- and Bear produces the compile_commands.json
file that Clang tooling (clangd, clang-tidy, and friends) needs to
understand your code:
bear -- make
Bear captures the compiler invocations while your build runs and writes the JSON compilation database for you. No changes to your build system are required.
When to use Bear
Bear is the tool of choice when the build system cannot produce a compilation database for you:
- Make, autotools, or custom script builds.
- A build you cannot or do not want to modify: a third-party project, a codebase you are just exploring, a CI job.
- Unusual toolchains: embedded, HPC, CUDA, cross-compilers, and compiler launchers such as ccache, distcc, and icecc.
- A build you cannot run at all: Bear can read
make -ndry-run output or a saved build log instead of running the build.
If your project uses CMake or Meson, those tools can export a compilation database directly, and Bazel has it through Hedron’s bazel-compile-commands-extractor. Use that when it is available; Generate compile_commands.json for a CMake project covers those exports and the cases where they are not enough.
Where to go next
- Getting started with Bear - the first successful run, start to finish.
- Install Bear - packages and building from source.
- Recipes - one page per task.
- Troubleshooting - when the database is empty or incomplete.
- How Bear works - the mechanism behind the results.
Looking something up rather than reading? Command-line options and Configure Bear are generated from Bear itself, so they match the version you have; Supported compilers lists every executable Bear recognizes.