-
Notifications
You must be signed in to change notification settings - Fork 79
Getting Started with analyzing CMake projects in Visual Studio
This page contains an overview of how to get started with analyzing CMake projects in VS2017 and VS2019, along with information about known issues. This feature is available starting with SLVS v4.38.
If you need more help, please open a ticket in the SonarLint - Get Help section of the community forum.
- Open / create a new CMake project in Visual Studio
- Add the command
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
to the top-levelCMakeLists.txt
file:
- Save the file.
- Check that VS has generated the compilation database.
Source and header files will now be analyzed on opening/saving. The SonarLint Output Window will contain more information about the progress of the analysis.
Note: if you are creating a new CMake project in VS2019 there appears to be an intermittent issue with VS not recognizing the project as a CMake project until the folder is closed and re-opened. See ticket #2656 for more information.
Follow the same getting started steps as for VS2017. In addition, to successfully analyze C/C++files in VS2017 your project will need to use a non-default CMakeSettings.json file. This is because the default CMakeSettings.json file uses the workspaceHash
or projectHash
macros, which are not supported in by SonarLint in VS2017.
- If you do not already have a CMakeSettings.json file on disc, Visual Studio will create one if you click on
Manage configurations option
and select a compilation target:
Visual Studio will then create a default CMakeSettings.json file:
- Edit the CMakeSettings.json file to change the
buildRoot
property to one that does not use eitherworkspaceHash
orprojectHash
.
Note: the defaults used by Visual Studio for buildRoot
and installRoot
in VS2019 have changed to:
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
Using those values in VS2017 will allow SonarLint to correctly analyze files in the CMake project.
VS2017 used a different hashing algorithm from VS2019, which is not supported. See issue #2632.
This means that SonarLint will not be able to analyse files if the buildRoot
property uses either of these macros.
SonarLint will not process environment variable overrides in CMakeSettings.json files.
The SonarLint analysis results may be incorrect if the overridden environment variables affect the CMake compilation e.g. changing the paths that are searched for header files.
See the Microsoft documentation for more information about support for CMake presets in Visual Studio.