28

I'm new to CMake and I'm trying to create the compile_commands.json file to use with clang, but I'm having some difficulties generating the file and I'm not sure why. I've been able to use cmake to compile the binary person that I have below, but after that was successful I've been unable to get it to output the compile commands.

I've also tried doing the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON flag, but that didn't work either. So far there's been no errors, but also no output.

Here's what my CMakeLists.txt file looks like:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(person Person.cc Pirate.cc main.cc)
Lucas
  • 2,514
  • 4
  • 27
  • 37
  • Which generator and version of CMake are you using? I've had trouble generating the compile_commands.json file using the XCode generator, but for Ninja and Makefiles it works ok. – alaroldai Jan 22 '15 at 00:01

3 Answers3

17

This ended up being an issue with using an old version of CMake. I ended up installing the newest version and it worked as expected.

According to Clang docs

"Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS."

Catskul
  • 17,916
  • 15
  • 84
  • 113
Lucas
  • 2,514
  • 4
  • 27
  • 37
  • 1
    What versions did it fail on, and what did you upgrade to? – Catskul Jan 22 '17 at 06:38
  • Haven't checked this thoroughly, but 2.6 definitely did not work, but anything >= 3.0 has seemed to work fine, so the feature was added somewhere in that range. – Lucas Jan 23 '17 at 20:01
  • 4
    Just found the exact answer here: http://clang.llvm.org/docs/JSONCompilationDatabase.html "Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS." – Catskul Jan 23 '17 at 21:21
  • Thanks for clarifying that. – Lucas Jan 24 '17 at 00:01
9

I also encountered the same problem as you. According to CMake doc

This option (CMAKE_EXPORT_COMPILE_COMMANDS) is implemented only by Makefile Generators and the Ninja. It is ignored on other generators.

Thus, there is no solution to generate compile_commands.json file when using MSVC.

Aaron Zhang
  • 111
  • 2
  • 2
7

I had the same problem, compile_commands.json was not generated with cmake, version 3.16.0. It was generated when I used the Ninja generator, but not Unix Makefiles.

That discussion gave me the fix:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # does not produce the json file
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") # works
PJ127
  • 986
  • 13
  • 23
  • You should use $ENV{} syntax to check/access environment variables such as CMAKE_* within `CMakeLists.txt`. https://stackoverflow.com/a/68724763/717355 – Philip Oakley Aug 10 '21 at 10:23
  • It's also a normal var: https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html – Jens A. Koch Apr 01 '23 at 23:38