我遵循了这个网页上的教程:https://code.visualstudio.com/docs/cpp/config-clang-mac,以便能够使用VS代码编译c++程序。在完成所有要求的事情后,我能够编译和调试c++文件。
然而,似乎存在一个问题。在“问题”框中,我有以下两个错误:
期望“;”在声明9,23末尾
基于范围的循环是一个C++11扩展-Wc++11-扩展
我的代码与VS代码网站中报告的代码完全相同。我还检查了所有*.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
}
}
]}
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}
编译时,一切都很好:
> 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.这是截图:

我使用的是macOS 10.15.6、VS代码1.47.1和C/C++扩展v0.29。
有什么想法吗?
发布于 2021-11-04 15:35:07
@RangerBob解决方案只是@brc所说的,但您不必创建一个新的项目/目录。转到Preferences > Search > "c++标准“。选择C++17,VS代码将创建一个新的文件"settings.json“与该标准导致3警告消失。
https://stackoverflow.com/questions/62938142
复制相似问题