Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake not generating compile_commands.json

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)
like image 319
Lucas Avatar asked May 30 '14 18:05

Lucas


2 Answers

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."

like image 160
Lucas Avatar answered Sep 28 '22 19:09

Lucas


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
like image 42
PJ127 Avatar answered Sep 28 '22 20:09

PJ127