适用于 macOS 的 VS code c++:编译工作正常,但 VS code 检测到错误

Ran*_*Bob 5 c++ visual-studio-code c++17

我按照此网页上的教程进行操作: https: //code.visualstudio.com/docs/cpp/config-clang-mac,以便能够使用 VS Code 编译 C++ 程序。完成所有要求的操作后,我能够编译和调试 C++ 文件。

不过,似乎有一个问题。在“问题”框中,我有以下两个错误:

预期的 ';' 在声明结束时 [9, 23]

基于范围的 for 循环是 C++11 扩展 [-Wc++11-extensions] [11, 29]

我的代码与 VS code 网站上报告的完全一样。我还检查了所有 *.json 文件,c++17 是默认编译器。

tasks.json 如下:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
  {
    "type": "shell",
    "label": "clang++ build active file",
    "command": "/usr/bin/clang++",
    "args": [
      "-std=c++17",
      "-stdlib=libc++",
      "-g",
      "${file}",
      "-o",
      "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
      "cwd": "${workspaceFolder}"
    },
    "problemMatcher": ["$gcc"],
    "group": {
      "kind": "build",
      "isDefault": true
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

}

c_cpp_properties.json如下:

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4
Run Code Online (Sandbox Code Playgroud)

}

编译时一切正常:

> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/stephane/Documents/c++/causality/run_mainfile.cpp -o /Users/stephane/Documents/c++/causality/run_mainfile <
Terminal will be reused by tasks, press any key to close it.
Run Code Online (Sandbox Code Playgroud)

这是屏幕截图:

在此输入图像描述

我使用的是 macOS 10.15.6、VS Code 1.47.1 和 C/C++ 扩展 v0.29。

有任何想法吗?

小智 2

@RangerBob 解决方案就是 @brc-dd 所说的,但您不必创建新的项目/目录。转到首选项 > 搜索 >“c++ 标准”。选择 C++17,VS Code 将使用该标准创建一个新文件“settings.json”,从而导致 3 个警告消失。