LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

bear

Generate compilation database for clang tooling

TLDR

Generate compilation database
$ bear -- make
copy
Append to existing database
$ bear --append -- make clean all
copy
Custom output file
$ bear --output [compile_commands.json] -- ninja
copy

SYNOPSIS

bear [options] -- build-command

DESCRIPTION

bear (Build EAR) generates a compilation database (compile_commands.json) by intercepting compiler calls during a build. This database enables tools like clangd, clang-tidy, and IDEs to understand the project structure without custom configuration.The tool works with any build system by recording actual compiler invocations. From version 3 onward, the build command must be separated from bear's own options by a `--`; older 2.4.x packages omit it.

PARAMETERS

-o file, --output file

Output file (default: compile_commands.json)
-a, --append
Append results to an existing database instead of overwriting it
-c file, --config file
Read settings (output formatting, compiler/source filters) from a configuration file
-h, --help
Print help information
-V, --version
Print version information

WORKFLOW

$ # With Make
bear -- make

# With CMake/Ninja
bear -- ninja

# Clean build
bear -- make clean all

# Append incremental build
bear --append -- make module
copy

FEATURES

- Build system agnostic- Intercepts compiler calls- Supports parallel builds- Works with make, ninja, etc.- No build system modification needed

COMPILATION DATABASE

Generated compile_commands.json format:

$ [
  {
    "directory": "/path/to/project",
    "command": "gcc -c file.c -o file.o",
    "file": "file.c"
  }
]
copy

INSTALL

sudo apt install bear
copy
sudo dnf install bear
copy
sudo pacman -S bear
copy
sudo apk add bear
copy
brew install bear
copy
nix profile install nixpkgs#bear
copy

CAVEATS

Requires running full build. May not capture all compilation units if cached. Some build systems have native support (CMake: -DCMAKEEXPORTCOMPILE_COMMANDS=ON). Different interception methods on different platforms.

HISTORY

bear was created by László Nagy (rizsotto) around 2012 to generate compilation databases for C/C++ projects using any build system. Version 3 introduced the `--` separator syntax, and version 4 rewrote the tool in Rust.

SEE ALSO

compiledb(1), cmake(1), clangd(1), make(1), ninja(1), clang(1)

RESOURCES

Copied to clipboard
Kai