VS代码,使用clang++,在一个C++文件中显示错误,但构建得很好。

问题描述 投票:0回答:1

这就解决了。我删除了 c_cpp_properties.json 并允许Vs Code通过运行命令重新生成它。c/c++ configuration (JSON)


我试图按照这里的说明,设置Vscode用于C++文件。

https:/code.visualstudio.comdocscppconfig-clang-mac。

而文件 构建并运行正常。

问题是我在文件中的两个地方得到了错误(红色方块)。其中一个是 "expected ;"(那是在声明了 vector<string> ),而另一个则写着类似 "基于范围的循环是C++11的扩展"

他们给出的样本文件是

#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 << " ";
    }
    cout << endl;
}

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

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
}
c++ visual-studio-code clang++
1个回答
0
投票

我解决了这个问题。我不知道具体是哪一步解决的,但我可以在这里为以后遇到这种情况的人提供一些有用的信息。

首先,要注意你启用了哪些C++扩展名。一开始坚持使用微软的CC++扩展(主要的)。

其次,确保扩展已经启用。似乎有一种方法可以在不启用扩展的情况下安装扩展。

第三,VS Code会重新生成某些 json 文件后,你把它们炸掉。例如,我删除了 c_cpp_properties.json 文件,然后从命令调色板中运行 "CC++编辑配置(JSON)",文件又回来了,可能解决了我的任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.