Configure IntelliSense with CMake Toolchain Files in Visual Studio 2019 16.9 Preview 2

Erika

Erika

Visual Studio can now configure IntelliSense in CMake projects based on the value of CMake variables set by CMake toolchain files. These improvements provide automatic IntelliSense configuration when a CMake toolchain file is used for configuration and build. For example, Visual Studio can now provide IntelliSense for CMake projects using an Android toolchain file. These changes will also improve IntelliSense for embedded developers using Visual Studio’s IoT (cross-compiler) configuration. More information on these changes is provided below. This support builds on Visual Studio’s native support for CMake and is available in Visual Studio 2019 version 16.9 Preview 2.

The following changes have been made to handle CMake toolchain files when using GCC or Clang. Make sure that the IntelliSense mode is set to any Clang or GCC option in the CMake Settings Editor (“Show advanced settings” at the bottom of the editor > “IntelliSense mode”). These changes apply to local Windows configurations and configurations targeting a remote Linux system or WSL. Visual Studio will introspect the following variables for extra IntelliSense information.

  1. If a CMake toolchain file defines CMAKE_CXX_COMPILER and CMAKE_C_COMPILER and writes the value to the CMake cache, then Visual Studio will query these compilers for the list of predefined DEFINES and the list of include search locations.
  2. If a CMake toolchain file defines CMAKE_SYSROOT and writes the value to the CMake cache, then Visual Studio will pass the value (−−sysroot=<value>) when determining the compilers defines and headers. If this variable is not found in the CMake cache, then Visual Studio next checks if the sysroot environment variable is defined and uses it instead.
  3. If a CMake toolchain file defines CMAKE_C_COMPILER_TARGET and CMAKE_CXX_COMPILER_TARGET and writes the value to the CMake cache, then Visual Studio will also pass these to the compiler (−−target=<value>).

Note that CMake scripts can write a variable to the CMake cache by appending CACHE <type> <docstring> to the set command.

Additionally, the following options have been added to CMakeSettings.json to enable further IntelliSense configuration. These options can be set per configuration.

"intellisenseOptions": {

        "additionalCompilerArgs": "<any-args>",

        "useCompilerDefaults": false

      }

additionalCompilerArgs can be used to explicitly add arguments to be used when Visual Studio queries the compiler for IntelliSense. useCompilerDefaults can be used to disable the use of the GCC/Clang compiler for IntelliSense configuration (defaults to true). These options apply to Windows, Linux, and WSL configurations.

Give us your feedback

Download Visual Studio 2019 version 16.9 Preview 2 today and give it a try. We’d love to hear from you to help us prioritize and build the right features for you. We can be reached via the comments below, Developer Community, and Twitter (@VisualC). The best way to file a bug or suggest a feature is via Developer Community.

2 comments

Leave a comment