Skip to content

CMAKE_EXPORT_COMPILE_COMMANDS requires two runs to produce compile_commands.json when set in CMakeLists.txt.

If setting CMAKE_EXPORT_COMPILE_COMMANDS on the command line, compile_commands.json is produced immediately as expected.

When CMAKE_EXPORT_COMPILE_COMMANDS is set inside a CMakeLists.txt file, running cmake the first time does not appear to produce compile_commands.json on it's first run . All subsequent runs after the first appear to produce the file as expected.

CMakeCache.txt gives some hints:

$ grep -r "EXPORT"
CMakeCache.txt:CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=OFF
CMakeCache.txt://ADVANCED property for variable: CMAKE_EXPORT_COMPILE_COMMANDS
CMakeCache.txt:CMAKE_EXPORT_COMPILE_COMMANDS-ADVANCED:INTERNAL=1

/usr/share/cmake-3.5/Modules/CMakeGenericSystem.cmake is the only place I could find it in stock Cmake code.

 if(CMAKE_GENERATOR MATCHES "Unix Makefiles")
    set(CMAKE_EXPORT_COMPILE_COMMANDS OFF CACHE BOOL
      "Enable/Disable output of compile commands during generation."
      )
    mark_as_advanced(CMAKE_EXPORT_COMPILE_COMMANDS)
  endif()

Text of example CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(cpp_skeleton CXX)

add_executable(main src/main.cpp)
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information