C++ build and debug with g++ in Windows Vscode

Windows Vscode上配置C/C++调试环境。

需要在项目文件夹新建.vscode/,新建以下两个文件:

tasks.json

  • tasks.json 用于C/C++ build
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++.exe build active file",

//custom the path of your g++
"command": "D:\\software\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-O0",
"-g",
"${file}",
// "-I /path/to/includeFile/" //can also set the include file path
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe" //generate the executable file with the same name
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

launch.json

  • launch.json 用于C/C++ Debug
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
32
33
34
35
36
{
"configurations": [
{
"name": "C/C++: 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",

//custom the path of gdb.exe
"miDebuggerPath": "D:\\software\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],

// build the program before debugging
"preLaunchTask": "C/C++: g++.exe build active file"
}
],
"version": "2.0.0"
}

screenshots.gif (1916×999)

debugger console

兼容gdb命令(需要加-exec),命令格式:

1
-exec <gdb command>

例如:

1
2
-exec p i
-exec b 10

reference

GDB, LLDB, and LLDB-MI Commands (GDB/LLDB)

For the C++ (GDB/LLDB) debugging environment, you can execute GDB, LLDB and LLDB-MI commands directly through the debug console with the -exec command, but be careful, executing commands directly in the debug console is untested and might crash VS Code in some cases.