Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable C++17 support in VSCode C++ Extension

I keep on getting error squiggles on std::string_view, but I am able to build just fine. Is there a way to tell intellisense or the C++ linter to use C++17?

The specific error I get is:

namespace "std" has no member "string_view"
like image 420
ajoseps Avatar asked Mar 21 '18 02:03

ajoseps


People also ask

How do I enable Extensions in VS Code?

You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.

Does VS Code support C?

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.

How do I get C to work on VS Code?

1. We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.

Why is C not working in VS Code?

You have to change it so it runs in the terminal. Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration.


3 Answers

This has become much easier now. Search for cppstandard in your vs code extension settings and choose the version of C++ you want the extension to use from the drop down.

enter image description here

In order to make sure your debugger is using the same version, make sure you have something like this for your tasks.json, where the important lines are the --std and the line after that defines the version.

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++ build active file",
      "command": "/usr/bin/g++",
      "args": [
        "--std",
        "c++17",
        "-I",
        "${fileDirname}",
        "-g",
        "${fileDirname}/*.cpp",
        "-o",
        "${workspaceFolder}/out/${fileBasenameNoExtension}.o"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ],
  "version": "2.0.0"
}

Note that if you're copying the above tasks.json directly, you'll need to have a folder named out in your workspace root.

like image 105
giraffesyo Avatar answered Oct 10 '22 14:10

giraffesyo


There's a posting in their GitHub issue tracker about this: std::string_view intellisense missing (CMake, VC++ 2017).

In another issue, it is said that the extension defaults to C++17, but does not yet support all of C++17 features: Setting C++ standard.

This is confirmed by c_cpp_properties.json Reference Guide, where an option is listed cppStandard which defaults to C++17. (To edit this file, press Ctrl + Shift + P and type in C/CPP: Edit Configurations).

It appears, then, they just don't have full support yet.

like image 28
Marc.2377 Avatar answered Oct 10 '22 14:10

Marc.2377


Just an updated. I got this issue as well.

I solve it by adding c_cpp_properties.json

  1. Ctrl + Shift + P then select C/C++:Edit Configurations (JSON)

  2. Adjust the content for cStandard and cppStandard:

        "cStandard": "gnu17",
        "cppStandard": "gnu++17",
    
like image 6
W Kenny Avatar answered Oct 10 '22 12:10

W Kenny