Install Bear for JSON Compilation Database on Ubuntu 24.04

Install Bear for JSON Compilation Database on Ubuntu 24.04

Bear is a command line tool that intercepts the build systems (for example, make) and automatically generates a compile_commands.json file, which is a standardized compilation database used by tools such as clangd for code navigation, autocompletion, and static analysis. This tutorial explains how to install Bear on Ubuntu 24.04.

Prepare environment

Usually, Bear is used when a build system does not natively support generating a JSON compilation database. It is often helpful with build systems like make. Before starting, make sure that both make and the GCC compiler are installed on the system.

sudo apt install -y gcc make

Install Bear

Update the package lists to ensure the latest information is available:

sudo apt update

Install Bear:

sudo apt install -y bear

The installed Bear version can be checked as follows:

bear --version

Testing Bear

Create a simple C source file:

nano main.c

Add the following code:

#include <stdio.h>

int main() {
    printf("Hello world\n");

    return 0;
}

Create a Makefile:

nano Makefile

Add the following content (make sure indentation uses a TAB, not spaces):

all:
    gcc main.c -o main

Run Bear with make to generate the JSON compilation database:

bear -- make

After the build completes, the generated file can be inspected:

cat compile_commands.json

Example output:

[
  {
    "arguments": [
      "/usr/bin/gcc",
      "-c",
      "-o",
      "main",
      "main.c"
    ],
    "directory": "/home/ubuntu",
    "file": "/home/ubuntu/main.c",
    "output": "/home/ubuntu/main"
  }
]

Uninstall Bear

If Bear is no longer needed, it can be uninstalled along with any unused dependencies:

sudo apt purge --autoremove -y bear

Leave a Comment

Cancel reply

Your email address will not be published.