Generate a compile_commands.json for a specific target
Setting CMAKE_EXPORT_COMPILE_COMMANDS ON
, ones can enable generating compile_commands.json
file containing the exact compiler calls for all translation units of the project in machine-readable form.
Imagine situation, when you have more than one target in your CMake project, and each target has its own defines, that are later used in #ifdef
s. Code analyzers like clangd read compile_commands.json
file, but if there are many targets, it will only select the first, so code completion and hilighting will be broken.
I suppose why compile_commands
may be generated at project configure and not before build, but I can sujjest new way, that will fix that problem: for each target there has to be generated its own compile_commands file, like, compile_commands_target1.json
and when --build
is called, contents of that files will be just copied to the main compile_commands.json
file depending on targets selected. So if all
targets are selected, then contents will be copied one-after-one