CMake

CMake is able to generate a compilation database by itself at configuration. When using a CMake project you have two ways of generating it:

The first variable can be set directly from CMake command using flag -DCMAKE_EXPORT_COMPILE_COMMANDS=ON , or directly in the root CMakeLists.txt file:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

However, if your CMake configuration has multiple targets, it is strongly recommended to activate compile commands generation only for your target, or it could lead to some undefined behaviour, if some sources are compiled multiple times with different commands in multiple targets.

To do that, start by forcing the global generation to off:

set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)

Then, activate the generation for your target:

set_target_properties(mytarget PROPERTIES EXPORT_COMPILE_COMMANDS ON)

When running CMake command, compile_commands.json is then generated in the build directory. E.g, when running command:

cmake -B build -DCMAKE_BUILD_TYPE=Release .

the file ./build/compile_commands.json is generated.

In beLow, just enter the command as a Configure script when asked to, and beLow will find the file by name.

Last updated