新装的VScode在运行C++11程序时,可能报错,例如这个程序:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
int b = 1;
auto a = b;
cout << a << endl;
vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
int len = msg.size();
cout << "长度" << len << endl;
for (int i=0; i<5; i++){
// ms.push_back("a");
cout << i << endl;
}
return 0;
}
此程序会报错以下错误:
expected';' at end of declaration gcc [Ln 10, Col 23] 'auto' type specifier is a C++11 extension [-Wc++11-extensions] gcc [Ln 8, Col 5]
此时,修改下tasks.json即可,打开tasks.json,将内容修改为:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-fdiagnostics-color=always",
"-g",
"-Wall",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
此时运行一切正常:
如果想在终端输出结果,进行debug即可:
本文最后更新于2024年3月10日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!