-
Notifications
You must be signed in to change notification settings - Fork 77
Getting Started with analyzing CMake projects in Visual Studio
The content on this page has moved: https://docs.sonarsource.com/sonarlint/visual-studio/getting-started/requirements/#getting-started-vs2019-and-later
The SonarLint documentation has moved! Please visit https://docs.sonarsource.com/sonarlint/visual-studio/ to have a look at the new documentation website. We’ve improved the documentation as a whole, integrated the four SonarLint IDE extension docs together, and moved everything under the sonarsource.com domain to share a home with the SonarQube docs (SonarCloud to come in Q3 of 2023).
These GitHub wikis will no longer be updated after October 1st, 2023 but no worries, we’ll keep them around a while for those running previous versions of SonarLint for Visual Studio.
This page contains an overview of how to get started with analyzing CMake projects in VS2017.3 and later versions, 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 VS2019 and later above. 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.
SonarLint uses the generated compilation database (compile_commands.json
) to fetch the compiler options to use during analysis.
The Ninja
generator creates a compilation - the other VS generators do not. Visual Studio introduce support for the Ninja
generator in VS2017 Update 3 (v15.3). Newer versions of VS use the Ninja
generator by default. You can also configure VS to use the Ninja
generator by providing an appropriate CMakeSettings.json
file e.g.
{
"configurations": [
{
...
"name": "x64-Release",
"generator": "Ninja",
...
"buildRoot": "${projectDir}\\out\\build\\${name}\\",
"installRoot": "${projectDir}\\out\\install\\${name}",
...
}
]
}