CMake
Last updated
Last updated
CMake is able to generate a compilation database by itself at configuration. When using a CMake project you have two ways of generating it:
Using variable globally (from CMake 3.5)
Using variable for the target you want to analyze and optimize with beLow (from CMake 3.20)
Compilation database generation with CMake is only available with Makefiles generators and Ninja generators. Any other generator will ignore these variables and the compilation database will not be generated.
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:
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:
Then, activate the generation for your target:
When running CMake command, compile_commands.json
is then generated in the build directory. E.g, when running command:
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.
Try not to leave any compile_commands.json
file in your project directory if you are able to generate it at configuration or build time, or it could be used in priority and lead to potential issues if not up to date or using absolute paths in copy mode.