整体参考官网文章
https://code.visualstudio.com/docs/cpp/config-mingw
上述常见问题整理如下:
- 在线的mingw-w64-install.exe安装失败,可以采用离线安装,离线安装包说明如下
https://sourceforge.net/projects/mingw-w64/files/
- 选择一个离线包下载就可以,见下图
- 下载解压到磁盘中后,需要在path添加路径,见下图
- 在一个文件夹中打开工程,这样后续生成的tasks.json和launch.json都是正确的,简单做法就是在对应工程-右键-vscode打开
- tasks.json和launch.json的配置,可以参考如下设置
//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": "g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
//launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}