繁体   English   中英

与 clang++ 一起使用的 VS 代码在构建良好的 C++ 文件中显示错误

[英]VS Code, used with clang++, shows errors in a C++ file that builds fine

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


我正在尝试按照此处的说明设置 Vscode 以与 C++ 文件一起使用:

https://code.visualstudio.com/docs/cpp/config-clang-mac

该文件可以正常构建和运行。

问题是我在文件中的两个位置出现错误(红色曲线)。 其中一个说“预期的”; (与声明的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++ 扩展。 首先坚持使用 Microsoft C/C++ 扩展(主要的)。

其次,确保扩展已启用。 似乎有一种方法可以安装扩展而不启用它们。

第三,VS Code 会在你吹走某些json文件后重新生成它们。 例如,我删除了c_cpp_properties.json文件,然后从命令面板运行“C/C++ 编辑配置 (JSON)”,文件又回来了,可能解决了我遇到的任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM