Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
        }
      ]
    }
  ]
}
like image 721
Gam Avatar asked Dec 17 '17 13:12

Gam


2 Answers

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

  • Developer Community: CMake C++ Project: Intellisense fails to parse included header directories
  • Developer Community: Intellisense shows false errors with C++17 and CMake
  • GitHub Microsoft/VSLinux Issue #131: openfolder project ignores intelliSenseMode
  • Visual C++ Team Blog: Use any C++ Compiler with Visual Studio
like image 184
Florian Avatar answered Nov 14 '22 22:11

Florian


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

like image 21
Merlyn Oppenheim Avatar answered Nov 14 '22 23:11

Merlyn Oppenheim