Top Related Projects
A static analyzer for Java, C, C++, and Objective-C
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
AddressSanitizer, ThreadSanitizer, MemorySanitizer
A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
Quick Overview
Bear is a tool that generates a compilation database for Clang-based tools by intercepting build commands. It's designed to work with various build systems and compilers, making it easier to use Clang tools on projects without modifying their build process.
Pros
- Works with multiple build systems (Make, CMake, Ninja, etc.) without configuration
- Supports various compilers (GCC, Clang, Microsoft Visual C++)
- Generates compilation databases for use with Clang-based tools
- Easy to integrate into existing build processes
Cons
- May not capture all compilation commands in complex build systems
- Requires running the build process to generate the compilation database
- Limited to C and C++ projects
- Can add overhead to the build process
Code Examples
Bear is not a code library, but a command-line tool. Therefore, there are no code examples to provide.
Getting Started
To use Bear, follow these steps:
- Install Bear on your system (e.g.,
apt-get install bearon Ubuntu) - Run your build command prefixed with
bear, like this:
bear -- make
- This will generate a
compile_commands.jsonfile in your current directory - Use the generated
compile_commands.jsonfile with Clang-based tools
For CMake projects, you can use the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON option instead of Bear:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
make
This will also generate a compile_commands.json file without using Bear.
Competitor Comparisons
A static analyzer for Java, C, C++, and Objective-C
Pros of Infer
- Supports multiple programming languages (Java, C/C++, Objective-C)
- More advanced static analysis capabilities, including interprocedural analysis
- Actively maintained by Facebook with regular updates and improvements
Cons of Infer
- Steeper learning curve and more complex setup process
- Requires more system resources due to its comprehensive analysis
- May produce more false positives in certain scenarios
Code Comparison
Bear (compilation database generation):
compilation_database = []
for command in commands:
compilation_database.append({
'directory': os.getcwd(),
'command': command,
'file': get_source_file(command)
})
Infer (running analysis):
infer run -- javac MyClass.java
infer run -- gcc -c example.c
infer run -- xcodebuild -target MyApp
Bear focuses on generating compilation databases for C/C++ projects, which can be used by various tools. Infer, on the other hand, is a standalone static analysis tool that can analyze code directly and provide more in-depth results across multiple languages.
While Bear is simpler to use and integrate into existing build processes, Infer offers more comprehensive analysis capabilities at the cost of increased complexity and resource usage. The choice between the two depends on the specific needs of the project and the desired level of analysis.
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
Pros of LLVM Project
- Comprehensive compiler infrastructure with multiple frontends and backends
- Extensive documentation and large community support
- Actively maintained with frequent updates and improvements
Cons of LLVM Project
- Significantly larger and more complex codebase
- Steeper learning curve for newcomers
- Requires more system resources to build and run
Code Comparison
Bear:
int main(int argc, char *argv[])
{
return bear_main(argc, argv);
}
LLVM Project:
int main(int argc, char **argv) {
InitLLVM X(argc, argv);
cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
return !runToolOnCode(new ClangTool, argv[1]);
}
Key Differences
- Bear focuses on generating compilation databases, while LLVM Project is a complete compiler infrastructure
- Bear is a smaller, more specialized tool, whereas LLVM Project offers a wide range of compiler-related functionalities
- Bear is easier to set up and use for specific tasks, while LLVM Project provides more flexibility and power for complex compiler operations
Use Cases
- Bear: Ideal for developers needing to generate compilation databases for existing projects
- LLVM Project: Suitable for compiler developers, researchers, and those working on large-scale code analysis or optimization projects
AddressSanitizer, ThreadSanitizer, MemorySanitizer
Pros of Sanitizers
- Comprehensive suite of dynamic analysis tools for detecting various runtime errors
- Developed and maintained by Google, ensuring high-quality and regular updates
- Supports multiple programming languages, including C, C++, and Go
Cons of Sanitizers
- More complex setup and integration process
- May introduce performance overhead during runtime analysis
- Requires recompilation of the codebase with specific flags
Code Comparison
Bear:
#include "bear/Bear.h"
int main() {
bear::Bear bear;
bear.run();
return 0;
}
Sanitizers:
#include <sanitizer/asan_interface.h>
int main() {
char *x = new char[10];
delete[] x;
return __asan_address_is_poisoned(x);
}
Summary
Sanitizers offers a robust set of tools for detecting runtime errors across multiple languages, backed by Google's development resources. However, it may require more setup and potentially impact performance. Bear, on the other hand, focuses on build command interception and is generally simpler to use but has a more specific use case. The choice between the two depends on the project's needs and the level of runtime analysis required.
A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
Pros of Catch2
- Header-only library, making it easy to integrate into projects
- Extensive test case and assertion macros for flexible testing
- Built-in command-line parser for test filtering and reporting options
Cons of Catch2
- Slower compile times due to its header-only nature
- Steeper learning curve for advanced features and customizations
Code Comparison
Catch2 test case example:
TEST_CASE("Addition works", "[math]") {
REQUIRE(1 + 1 == 2);
REQUIRE(2 + 2 == 4);
}
Bear usage example:
bear -- g++ -c main.cpp
Key Differences
- Bear is a build intercept tool for generating compilation databases, while Catch2 is a unit testing framework
- Bear focuses on capturing compiler commands, whereas Catch2 provides a framework for writing and running tests
- Bear is primarily used in the build process, while Catch2 is used in the testing phase of development
Use Cases
- Use Bear when you need to generate a compilation database for tools like clang-tidy or IDE integrations
- Choose Catch2 when you want a modern, feature-rich unit testing framework for C++ projects
Community and Support
- Catch2 has a larger community and more frequent updates
- Bear has fewer contributors but is actively maintained for its specific use case
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ÊÂ·á´¥Â·Ê Build EAR
Bear gives you working code navigation, autocomplete, and diagnostics on
any C/C++ project, whatever the build system. Prefix your build command
with bear -- and it produces the compile_commands.json file that
Clang tooling (clangd, clang-tidy, and friends) needs to understand your
code.
The JSON compilation database describes how each translation unit is compiled. Bear captures the compiler invocations while your build runs and writes the database for you - no changes to your build system 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 - most of the C world.
- 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 (ARM, TI, Microchip, QNX), HPC (Cray, Intel, NVIDIA, MPI wrappers), CUDA, cross-compilers, and compiler launchers such as ccache, distcc, and icecc.
- A build you cannot run at all: parse
make -ndry-run output or a saved build log instead.
Bear can intercept the build as it runs, recording what the compiler was actually invoked with, including flags injected by wrappers and entries for generated sources. Bear can also interpret the commands a build system plans to run, formatting those into a compilation database as if they had executed.
If your project uses CMake, Meson, or Bazel, those tools can export a compilation database directly; use that when it is available.
How to install
Bear is packaged for many distributions. Check your distribution's package manager first. Alternatively, you can build it from source.
How to use
After installation, run:
bear -- <your-build-command>
Bear writes compile_commands.json to the current working directory. Pass
Bear's own options before --; everything after that is treated as part of
the build command.
If you cannot run the build - all you have is a CI log or the build system's dry-run output - Bear can reconstruct the database from that text instead:
make -n | bear parse-sh
For more, run bear --help or read the man page. The
documentation site covers limitations, known issues, and
platform- and toolchain-specific usage.
Limitations
Bear works on Linux, macOS, FreeBSD, OpenBSD, NetBSD, DragonFly BSD, and Windows.
Each operating system and build system imposes constraints on how Bear can run and which interception method is available, and those affect the final output. The documentation site describes the available options and each option's limitations.
Acknowledgments
Bear is kept going by people who chip in. Thank you.
- @maflcko for regular sponsorship.
- Anthropic for a free Claude subscription, which supported development work.
Problem reports
Before opening a new problem report, please check the documentation to see if your problem is a known issue with a documented workaround. It's also helpful to look at older (possibly closed) issues before opening a new one.
If you decide to report a problem, please provide as much context as possible to help reproduce the error. If you just have a question about usage, please don't be shy; ask your question in an issue or in our chat.
If you've found a bug and have a fix for it, please share it by opening a pull request.
Please follow the contribution guide when you do.
Top Related Projects
A static analyzer for Java, C, C++, and Objective-C
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
AddressSanitizer, ThreadSanitizer, MemorySanitizer
A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot