Getting Started with analyzing CMake projects in Visual Studio - SonarSource/sonarlint-visualstudio GitHub Wiki

Information

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.

Getting started - VS2019 and later

  1. Open / create a new CMake project in Visual Studio
  2. Add the command set(CMAKE_EXPORT_COMPILE_COMMANDS ON) to the top-level CMakeLists.txt file:

Adding Set command to CMakeLists.txt

  1. Save the file.
  2. 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.

Getting started - VS2017.3

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.

  1. 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:

Selecting manage config dropdown in VS2017

Visual Studio will then create a default CMakeSettings.json file:

Default CMakeSettings.json file

  1. Edit the CMakeSettings.json file to change the buildRoot property to one that does not use either workspaceHash or projectHash.

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.

CMake - known limitations

VS2017: workspaceHash and projectHash macros are not supported in VS2017

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.

Environment variable overrides in CMakeSettings.json are not supported

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.

CMake presets are not currently supported by SonarLint

See the Microsoft documentation for more information about support for CMake presets in Visual Studio.

Only the Ninja generator is supported

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}",
      ...
    }
  ]
}