45

I've been trying to use clang-modernize with CMAKE_EXPORT_COMPILE_COMMANDS as recommended in the help of this tool.

With this option cmake generates a JSON file containing compile info like include paths (see also).

This variable is accepted on the command line of cmake, but cmake --help-variable CMAKE_EXPORT_COMPILE_COMMANDS doesn't work (which is coherent with this mailing list posting).

Has someone any idea on how to use it?

I could also use it with cppcheck.

Some more info

I've discovered on a clang developer forum that this cmake feature is not available on all generators. This might change in the future, in the mean time my question remains and I will try too see what happen if I use other generators than Visual Studio.

maxschlepzig
  • 31,679
  • 12
  • 125
  • 164
dzada
  • 4,644
  • 5
  • 26
  • 36

3 Answers3

55

I suggest setting

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

in the CMakeList.txt

Jörn Reimerdes
  • 859
  • 7
  • 10
  • 11
    late to the show but for those also googling this, that would never have worked - it has to be set prior to the invocation of cmake. A 2nd cmake invocation would see it work however. – RichieHH Mar 06 '19 at 14:16
  • @HörmannHH Thanks for mentioning this. I had exactly this issue where compile_commands.json was never generated the first time, but only after the second configure. – Marnix Dec 16 '19 at 16:09
  • 1
    Continuing my search I found this issue https://gitlab.kitware.com/cmake/cmake/issues/16588. I was able to make it happen in CMakeLists.txt if I put it right after my `project()` specification. – Marnix Dec 16 '19 at 16:23
  • In the (now closed) issue linked by @Marnix, there was another cause for this issue that came from CMake overwriting the variable, even if `project()` was called before the variable was set. This was resolved for [CMake 3.18+](https://gitlab.kitware.com/cmake/cmake/-/commit/fe19df49); for older versions, a workaround can be implemented by modifying the `set()` command to `set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")`. – Das_Geek May 18 '21 at 19:50
29

As of CMake 3.5 the CMAKE_EXPORT_COMPILE_COMMANDS option is supported by the Ninja and Makefiles generators.

That means to generate a JSON compile database one has to select a generator that supports it.

For example on UNIX just:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src

(as it uses the makefile generator there, by default)

Otherwise you can explicitly specify a generator like this:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G Ninja

Or:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G 'Unix Makefiles'

Or another makefiles variant that your cmake supports - a list of supported generators is included in the output of cmake --help.

Note that the compile database JSON file is generated at cmake execution time - not at compile time. Also, with recent clang versions (e.g. clang >= 3.8), clang-modernize was merged into clang-tidy.

maxschlepzig
  • 31,679
  • 12
  • 125
  • 164
5

I too was not able to get to work on the Visual Studio generator. It did, however, work using the "NMake Makefiles" generator.

C:\work\build>cmake -G "NMake Makefiles"  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON  ..
zr.
  • 7,240
  • 10
  • 48
  • 81
  • 2
    Yep. As of Cmake 3.15.5: ["This option is implemented only by Makefile Generators and the Ninja. It is ignored on other generators."](https://cmake.org/cmake/help/v3.15/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html) – sunny moon Nov 26 '19 at 14:45