新装的VScode
在运行C++11
程序时,可能报错,例如这个程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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; } |
此程序会报错以下错误:
1 2 |
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
,将内容修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
{ "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
即可: