Sonarlint CMake compile_commands.json in workspace

Please provide

  • Operating system: Windows 10 Pro
  • SonarLint plugin version: Sonarlint v4.4.2
  • Programming language you’re coding in: C
  • Is connected mode used: No

Question : How do I achieve generating a compile_commands.json in my workspace with multiple C files in different folders inside my workspace?

Example :

Work Space folder

CMakeList.txt

Folder 1

C File 1
C File 2

Folder 2

C File #3
C File #4

Issue : Setup does not create compile_commands.json for all 4 files, unless CMakeLists.txt is inside each folder or all the files are in the same directory of the CMakeList, however doing so would be inefficient.

CMakeList.txt Content:

cmake_minimum_required(VERSION 3.5)

project(Project Folder)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(SOURCE_DIR “${CMAKE_SOURCE_DIR}”)

file(GLOB SOURCES “${SOURCE_DIR}/*.c”)

add_executable(${PROJECT_NAME} ${SOURCES})

Hi @Shawn_Marie_Catana and thanks for sharing the question,

If I understand correctly, your question is more about writing a CMakeLists.txt file to build your project. Once you have that, generating the compilation database is as easy as setting CMAKE_EXPORT_COMPILE_COMMANDS.

Please note that you don’t need to write a CMakeLists.txt file just to generate a compilation database file for your project. We have listed many options to generate a compilation database in our docs.

To answer your CMake question, assuming that your example has CMakeLists.txt in the same level of nestedness as Folder 1 and Folder 2, then the glob pattern in your file command needs to account for the directories as far as I can see:

file(GLOB SOURCES "${SOURCE_DIR}/*/*.c")

Please also note that it is generally not recommended to use glob to collect sources. See the official docs.

I hope this helps,

Best regards,
Michael