Please provide
- Operating system: Windows 10 Pro
- SonarLint plugin version: Sonarlint v4.4.2
- Programming language you’re coding in: C & C++
- Is connected mode used: No
Question : Is there a CMake command that would help build a compile_commands.json file that checked all my projects inside my workspace?
Additional Note : I’m currently working on 2 projects and they are C and C++ respectively, I am using Sonarlint for C and C++, but it’s tedious/inefficient to create CMake builds for each project and constantly changing the file path for the compile_commands.json that is required. Is there a possible way that I can just make one compile_commands.json for both?
Hi @Shawn_Marie_Catana and thanks for sharing the question with us,
I’m currently working on 2 projects and they are C and C++ respectively.
I would start by considering two simple alternatives here. Let me know if neither works for you and why:
-
If the projects depend on one another, I would try to have a top-level CMakeLists.txt
for both of them (while keeping the targets separate for each project). This way, the top-level CMakeLists.txt
can be used to generate a compilation database (and build) both projects.
-
If the projects are separate, and therefore there is no reason to have a shared CMakeLists.txt
for them. I would consider using them in separate VSCode workspaces.
The reason I would recommend one of the above solutions first, is that all the tools (CMake, SonarLint, any VSCode extensions, …) have the same view of your project(s). If you can’t use any of them for some reason, you can try, as a last resort, one of the following options:
-
Combine the arrays from both compile_commands.json
files into one file (manually or using a custom script) and use that in SonarLint instead.
-
Generate your single compile_commands.json
file using a different method. For example by building both projects under Bear.
I hope this helps,
Best regards,
Michael
In my current case it’s the latter of the 2 suggestions. However, by creating a CMakeLists.txt on the root folder of my project, it cannot find the C files that I want to be created a compile_commands.json of for the Sonarlint Compilation database.
This is currently my workspace setup for the project :
Project Folder
Function Folder
C file to check with Sonarlint
And this is the CMakeLists.txt command I am using :
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, thanks for sharing the problem.
In my current case it’s the latter of the 2 suggestions.
So did you switch to using them in two separate VSCode workspaces? Or do you still prefer to open them in a single workspace?
it cannot find the C files that I want
I am afraid I would need to understand the file structure of your project (and where the CMakeLists.txt
files you have are located) to help with the CMake file. Your description suggests to me that the .c
files you are looking file are not located under ${SOURCE_DIR}
. You may want to try to add some message
lines to make sure that the values of these variables match your expectations, and do the necessary modifications…
I hope this helps,
Best regards,
Michael
do you still prefer to open them in a single workspace?
I would still prefer this actually
Workspace Folder
CMakeList.txt
Project Folder 1
.c file
Project Folder 2
.c file
This my folder structure, and I would like to preferably have only 1 compile_command.json for both .c files in different projects.
Hi @Shawn_Marie_Catana, I think I answered this question in your other thread. Let me know if you have further questions…
I hope this helps,
Best regards,
Michael
1 Like