下载网址:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/
下载压缩包——×86_64-posix-sjlj 或 ×86_64-posix-seh(速度快)
#include
#include
#include
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
您还可以通过在主文件菜单中查看"自动保存"来自动保存文件更改。
tasks.json文件告诉 VS 代码如何构建(编译)程序。此任务将调用 g++ 编译器,以创建基于源代码的可执行文件。
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe"
}
],
"version": "2.0.0"
}
点击helloworld.cpp文件
按Ctrl+Shift+B为了获得helloworld.exe文件
任务开始时,您应该看到集成终端面板显示在源代码编辑器下面。任务完成后,终端显示编译器的输出,指示构建是成功还是失败。对于成功的 g++
构建,输出看起来是这样的:
接下来,按下 F5(调试程序),从主菜单中,选择运行®>添加配置,然后,您将看到各种预定义调试配置的下拉。选择g++.exe构建和调试活动文件。
VS 代码创建一个名为launch.json的文件,并在编辑器中打开它,并构建和运行"helloworld"。
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
注:确保launch.json文件的preLaunchTask的值与tasks.json文件的label值一致(一般都一致)
地址:https://code.visualstudio.com/docs/cpp/config-mingw