Skip to main content

Compilation Database Generation

Quick guide for generating the compile_commands.json file on Linux

For Windows: see the list of available tools below.

For this guide we will use the Himeno Fortran example from our Codee demos repository.

The generation of the compilation database depends on the particular build system used. On Linux, the most typical are Makefiles and CMake:

  • CMake: Add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to your usual CMake invocation; for instance:

    cmake . -DCMAKE_Fortran_COMPILER=gfortran -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G "Ninja" -B build
  • Makefiles: Since they do not natively support the generation of compilation databases, we recommend using bear. This tool is already bundled in the Codee package for Linux.

    To generate the compilation database, prepend bear -- to your typical make invocation; for example:

    bear -- make

Himeno supports both CMake and make.

Any of the above commands will generate a compile_commands.json file containing the compiler invocation for each file in the project. Since Himeno is a single-file project, the compilation database will have a single entry:

build/compile_commands.json
[
{
"directory": "/home/user/codee-demos/Fortran/Himeno/build",
"command": "/usr/bin/gfortran -c /home/user/codee-demos/Fortran/Himeno/himeno.f90 -o CMakeFiles/himeno.dir/himeno.f90.o",
"file": "/home/user/codee-demos/Fortran/Himeno/himeno.f90"
}
]

Now you can use the JSON generated to substitute the compilation command in the Codee invocation. For example:

Codee command
codee checks -p build/compile_commands.json

Complete list of Tools and build systems that provide compile_commands.json generation features

Build system / Meta-build systemC/C++FortranLinuxWindowsClarifications
CMake✔** Only when using the Ninja generator
Ninja
Fortran Package Manager
Scons✘** DB file generation feature limited to C/C++
meson✔** Only when using the default Ninja generator
Bazel
ToolC/C++FortranLinuxWindowsDescription
BearIntercepts build commands on the fly and generates the DB file (very useful for legacy Makefile projects)
Clang Power ToolsVisual Studio extension (limited to Clang compiler)

Note: An improved version of Bear is already provided in the Codee package (for Linux).

If none of the above works you could also try these old tools, although they are deprecated and without support for years: