Thanks to visit codestin.com
Credit goes to docs.codee.com

Skip to main content

Compilation Database Generation

Goal

To analyze a complete project, you need a compilation database containing all the compiler instructions for its source files. In this guide, we walk you through generating a compile_commands.json file , demonstrated with a Fortran open source project.

Getting ready

For this demonstration, we will use the Fortran implementation of the Himeno benchmark, a Poisson equation solver. Start by cloning the repository:

git clone https://github.com/codee-com/codee-demos.git

Navigate to the source code directory:

cd codee-demos/Fortran/Himeno

Third party tools to generate the compile_commands.json

Tip

To analyze a complete project with many source files, you need a compilation database, which is a file typically named compile_commands.json containing all the compiler invocations for the source files. It provides Codee with information about the name, version and location of the compiler in the system, required compiler flags, and location of the target source files.

For Linux, the recommendation is to use Bear.

For Windows, the recommendation is to use CMake with Ninja generators.

LinuxCompilersCompiler wrappersWindows
bear
CMakecodee CLINinja generator
Warning

Ensure you are using the bear tool distributed with Codee, not other installations of bear, in order to take advantage of its enhanced support for compilers (e.g. GNU, LLVM, Intel, AMD, Nvidia, Cray) and compiler wrappers (e.g. MPI wrappers in supercomputers, Microsoft wrappers).

The recommended way of generating the compilation database is to use bear, which comes bundled with Codee packages. bear is able to intercept the compiler calls made by your build system in order to generate the compile_commands.json file. To use it you only need to prepend bear -- to your typical build command.

To generate it for the Himeno project copy the following command:

bear -- make

Since Himeno is a single-file project, the compilation database will have a single entry:

compile_commands.json
[
{
"arguments": [
"/usr/bin/gfortran",
"-c",
"-O3",
"-I",
"/usr/include",
"-I",
".",
"-o",
"himeno",
"himeno.f90"
],
"directory": "/home/user/codee-demos/Fortran/Himeno",
"file": "/home/user/codee-demos/Fortran/Himeno/himeno.f90",
"output": "/home/user/codee-demos/Fortran/Himeno/himeno"
}
]

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

Codee command
codee checks --check-id PWR069 -p compile_commands.json
Codee output
Date: 2025-11-06 Codee version: 2025.4 License type: Professional

Dependency processing... Done (10 ms)

[1/1] himeno.f90 ... Done

CHECKS REPORT

himeno.f90:137:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:165:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:224:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:256:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:276:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module

SUGGESTIONS

Use --check-id and --verbose to focus on specific subsets of checkers, e.g.:
codee checks --check-id PWR069 --verbose -p compile_commands.json

1 target file, 7 functions, 5 loops, 214 LOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 174 ms

Using CMake (Alternative)

Warning

Ensure you are using CMake version 3.5 or higher.

As an alternative, CMake has its own way of obtaining the compile_commands.json file.

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

The compile_commands.json file will be now stored in the build/ directory and it will look like this:

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",
"output": "CMakeFiles/himeno.dir/himeno.f90.o"
}
]

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

Codee command
codee checks --check-id PWR069 -p build/compile_commands.json
Codee output
Date: 2025-11-06 Codee version: 2025.4 License type: Professional

Dependency processing... Done (10 ms)

[1/1] /home/user/codee-demos/Fortran/Himeno/himeno.f90 ... Done

CHECKS REPORT

/home/user/codee-demos/Fortran/Himeno/himeno.f90:137:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:165:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:224:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:256:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:276:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module

SUGGESTIONS

Use --verbose to get more details, e.g:
codee checks --verbose --check-id PWR069 -p build/compile_commands.json

1 file, 7 functions, 5 loops, 214 LOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 182 ms