Gam
Gam

Reputation: 1272

enable c++17 intellisense open folder visual studio ninja-clang

Using "-Xclang -std=c++17" I can build the executable, however I can't find what activates c++17 intellisense. I've tried many combinations as shown below and none seem to work

CMakeLists.txt

cmake_minimum_required(VERSION 3.9.2)
set(CMAKE_CXX_STANDARD 17)
project(myapp)
add_compile_options("-Xclang" "-std=c++17")
add_executable(myapp main.cpp)
set_target_properties(myapp PROPERTIES CXX_STANDARD 17)
target_compile_features(myapp PRIVATE cxx_std_17)

main.cpp

#include <tuple>
namespace test1::test2 // red [qualified name is not allowed]
//       ^^^^^^^^^^^^^
{}

int main()
{
    auto[a, b] = std::pair<int, int>();
    //  ^^^^^^
    return 0;
}

CMakeSettings.json

{
  // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "clang-cl"
        },
        {
          "name": "CMAKE_C_COMPILER",
          "value": "clang-cl"
        },
        {
          "name": "CMAKE_SYSTEM_NAME",
          "value": "Windows"
        }
      ]
    }
  ]
}

Upvotes: 7

Views: 3791

Answers (3)

Dr. Andrey Belkin
Dr. Andrey Belkin

Reputation: 803

For my ARM/Linux development, I have added "intelliSenseMode": "linux-gcc-arm" into CMakeSettings.json as follows:

{
  "configurations": [
    {
      "name": "Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "cmakeToolchain": "arm-toolchain.cmake",
      "intelliSenseMode": "linux-gcc-arm",
      "inheritEnvironments": []
    },
    {
      "name": "Release",
      "generator": "Ninja",
      "configurationType": "Release",
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "cmakeToolchain": "arm-toolchain.cmake",
      "intelliSenseMode": "linux-gcc-arm",
      "inheritEnvironments": [],
      "variables": []
    }
  ]
}

and now Visual Studio correctly parses the source code.

All possible values are:

windows-msvc-x86
windows-msvc-x64
windows-msvc-arm
windows-msvc-arm64
android-clang-x86
android-clang-x64
android-clang-arm
android-clang-arm64
ios-clang-x86
ios-clang-x64
ios-clang-arm
ios-clang-arm64
windows-clang-x86
windows-clang-x64
windows-clang-arm
windows-clang-arm64
linux-gcc-x86
linux-gcc-x64
linux-gcc-arm

Upvotes: 0

Merlyn Oppenheim
Merlyn Oppenheim

Reputation: 21

To enable correct Intellisense for C++17, specify set(CMAKE_CXX_STANDARD 17) in CMakeLists.txt before the project definition; no other configuration is needed. CMake will supply -std=c++17 for clang-cl at build time.

cmake_minimum_required (VERSION 3.9.2)

set(CMAKE_CXX_STANDARD 17)

project ("test")
add_executable (Test "test.cpp" "test.h")

Verified in Visual Studio 2017 15.9.8

Upvotes: 1

Florian
Florian

Reputation: 42872

As for December 2017 the only way to define the IntelliSense mode is via a CppProperties.json file in your root folder, which you can't combine with CMakeSettings.json.

See comments under Visual C++ Team Blog: Customizing your Environment with Visual C++ and Open Folder:

  • justanotherdev: "... Would it be possible to inherit CppProperties includes from the project created via CMake? If so, getting Linux intellisense from the Windows CMake project would be a breeze and would solve a major issue with Linux (needing to specify all the includes for a project manually)."
    • Will Buik [MSFT]: "This isn’t supported today. ..."

I've tried it and had no luck using something similar to what's recommended in the "Open Folder projects in Visual C++" documentation.

  1. I did go to Project / Edit Settings / CppProperties.json

    enter image description here

  2. And inserted for testing into my configurations something like

    ...
        "compilerSwitches": "/std:c++17",
        "intelliSenseMode": "windows-msvc-x86"
    ...
    

    or any other of the supported modes:

    enter image description here


References

Upvotes: 2

Related Questions