Mac VSCode 调试器总是显示有关“;”的错误 和 ':'

Aar*_*ang 5 c++ c++11 visual-studio-code vscode-debugger

我正在尝试在我的 Mac 上设置 vscode 环境。我按照网站上的步骤进行操作https://code.visualstudio.com/docs/cpp/config-clang-mac 但是当我尝试调试我的程序时,它显示错误并且无法跳过。我不确定是因为版本还是其他什么原因。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> msg {"Hello", "C++++", "World", "from", "VS Code", "and the C++ extension!"};

for (const string& word : msg)
{
    cout << word << " ";
}

vector<string> aaa {"H", "E", "L", "L", "O"};
for (const string& word : aaa)
{
    cout << word << " ";
}


cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

并且错误消息显示

预计 ; 在声明的末尾 [9, 23] 期望 ; 声明末尾 [16, 23] 基于范围的 for 循环是 c++11 扩展 [11, 29] 基于范围的 for 循环是 c++11 扩展 [17, 29]

我的 launch.json 文件是

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
    // "name": "(lldb) Launch",
        "name": "clang++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "lldb",
        "preLaunchTask": "clang++ build active file"
    }
]
}
Run Code Online (Sandbox Code Playgroud)

我的task.json文件是

{
"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)

小智 6

这个错误是判断造成的problemMatcher

因为它没有判断出正确的C++版本。

解决方法如下。

  • 设置将打开
  • 找到code-runner: Executor Map并点击编辑settings.json
  • 找到并在后面cpp添加 像这样 -std=c++17cd $dir && g++"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • 保存它,你就完成了!你可以再试一次。


Bil*_*ill 1

你让它发挥作用了吗?我在 .vscode 目录中添加了 c_cpp_properties.json,内容如下:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
            ],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/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)

我能够调试相同的代码示例