新装的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即可:

马春杰杰



最新评论
站长您好,亚马逊云咨询推广资源,望建立联系,可邮件,谢谢。
换友情链接吗?
看你的站做的挺不错的
恭喜!!太强了,硕博连读啊
雁过留毛,人过留名。
看不懂但大受震撼
每天都在战争,希望2026和平.
ZeroTier 看过多篇帖子,目前群辉、飞牛os、win11 、安卓、ubuntu ≥18.04 顺利通过,这篇相对于简单 实用、特来感谢 🙄