【问题标题】:Mac VSCode Debugger always show error about ';' and ':'Mac VSCode 调试器总是显示关于 ';' 的错误和 ':'
【发布时间】:2020-07-13 21:35:01
【问题描述】:

我正在尝试在我的 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;
}

并且错误信息显示

期待;在声明结束时 [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"
    }
]
}

我的 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
        }
    }
]
}

【问题讨论】:

  • 请不要发布代码或错误消息的图像。请参阅this Meta post 了解众多原因的列表。代码和错误消息都是文本信息,应以该形式直接复制/粘贴到您的问题中。
  • 你需要将 c++17 标志添加到 intellisense 编译器中(虽然我不能说如何在 VScode 中做到这一点)
  • 我不确定你的意思。你能说得更详细一些吗?
  • 这里解释了这个问题的解决方案stackoverflow.com/questions/55116344/…

标签: c++ c++11 visual-studio-code vscode-settings vscode-debugger


【解决方案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
}

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

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。但问题是默认情况下的 C/C++ 配置不是教程中显示的。因此,如果您一步一步地按照教程进行操作,您最终可能不会配置要使用的 c/c++ 编译器。我所做的是跳过调试步骤,直接进入 C/C++ 配置步骤,然后添加 .vscode/c_cpp_properties.json,如教程中所示(也是上面帖子中的@Bill)。 然后如果您尝试调试,它不会显示该错误,因为现在您已将编译器配置为 c++17。

    【讨论】:

    • 我尝试了您的建议,如果我构建文件,它不会显示错误,但是如果我使用 VSCode 的调试按钮对其进行调试,它会显示。我的 tasks.json 与 VSCode 说明中的文件完全相同。正如您提到的,我还完成了配置步骤。还有什么建议吗?
    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多