【发布时间】:2020-07-16 15:29:20
【问题描述】:
我按照这个网页上的教程:https://code.visualstudio.com/docs/cpp/config-clang-mac 能够使用 VS Code 编译 c++ 程序。完成所有要求的事情后,我就可以编译和调试 c++ 文件了。
但是,似乎有一个问题。在“问题”框中,我有以下两个错误:
预期的';'在声明结束时 [9, 23]
基于范围的 for 循环是 C++11 的扩展 [-Wc++11-extensions] [11, 29]
我的代码与 VS code 网站上报告的完全一样。我还检查了所有 *.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 Code 1.47.1 和 C/C++ 扩展 v0.29。
有什么想法吗?
【问题讨论】:
-
听起来您实际上并没有在 C++17 模式下运行编译器。在问题中显示完整的构建设置和生成的命令行。
-
您是否安装了 Intellisens 或在您键入时添加自动完成和语法检查的类似插件?可能是该工具使用了与您的编译器不同的设置,并且报告了不同的错误。
-
我为 VS Code 安装了 C++ 扩展,包括 IntelliSense。我现在还报告了一些 *.json 文件。
-
@RangerBob 你的配置文件对我来说很好。尝试重新启动 VSCode,看看问题是否仍然存在。
-
@RangerBob 现在没有什么合适的。配置似乎一切都很好。您可以尝试一件事:创建一个新目录。用 vscode 打开它,然后打开首选项 > 搜索 > “c++ 标准”。选择可用的 C++17 或 C++20。什么都不做。创建您的文件并检查问题是否仍然存在。我知道这不是“解决方案”,尽管您可以尝试一下,但不会造成太大伤害。如果仍然存在,请在此处提出问题:github.com/microsoft/vscode-cpptools/issues.
标签: c++ visual-studio-code c++17