LinuxCommandLibrary

bear

Generate compilation database for clang-based tools

TLDR

Generate compile_commands.json by running a build command

$ bear -- [make]
copy

Generate compilation database with a custom output file name
$ bear --output [path/to/compile_commands.json] -- [make]
copy

Append results to an existing compile_commands.json file
$ bear --append -- [make]
copy

Run in verbose mode to get detailed output
$ bear --verbose -- [make]
copy

Force bear to use the preload method for command interception
$ bear --force-preload -- [make]
copy

SYNOPSIS

bear [OPTION]... [--] <BUILD-COMMAND> [<args>...]

PARAMETERS

--append, -a
    Append to output file instead of overwriting

--cdb-only
    Generate DB only; skip build execution

--config <FILE>
    Path to Bear config file

--format-version <N>
    JSON format version (1 or 2)

--help, -h
    Print usage summary

--list-config
    List available config keys

--no-config
    Ignore Bear config files

--output <FILE>, -o <FILE>
    Output file (default: compile_commands.json)

--use-file, -f
    Read args from file instead of command line

--version, -v
    Print version

--
    End of Bear options

DESCRIPTION

Bear is a utility that generates a JSON compilation database (compile_commands.json) by hooking into build processes. It uses LD_PRELOAD to intercept invocations of compilers like gcc, clang, and linkers, capturing the exact commands used to build each source file. This database enables tools like clangd, ccls, or IDEs to provide accurate code intelligence, diagnostics, and refactoring.

Invoke it by prefixing your build command, e.g., bear make or bear ninja. It supports Make, Ninja, CMake, Meson, and others without modification. Bear executes the build and outputs the database in the current directory by default.

Key benefits include retrofitting legacy build systems for modern tooling and automating CDB generation in CI. It handles parallel builds and complex arguments faithfully. Limitations apply to non-standard compilers or builds avoiding dynamic linking.

CAVEATS

LD_PRELOAD hooking may fail with static builds, non-gcc/clang compilers, or setuid binaries. Requires compiler dev packages. Parallel builds supported but verify completeness.

EXAMPLE

bear -- make all
bear --output build/compile_commands.json -- ninja

INSTALLATION

Debian/Ubuntu: sudo apt install bear
Fedora: sudo dnf install bear
From source: meson setup build && ninja -C build install

HISTORY

Developed by RĂ³bert Nagy starting 2014; open-sourced on GitHub (rizsotto/Bear). Gained popularity with LSP rise (~2018+). Actively maintained; v3.2+ supports JSON schema v2.

SEE ALSO

make(1), ninja(1), cmake(1)

Copied to clipboard