纸上得来终觉浅,绝知此事要躬行。
- 陆游 《冬夜读书示子聿》

VS Code 配置 LinuxC/C++ GDB调试环境

2026-04-13 22:53 · 使用教程 · 2 minutes read

1. 安装c/c++插件

iamge-00

2. 编写debug配置文件

  1. 点击debug按钮,然后点击 create a launch.json file 链接 ,如下图所示

image-01

2, vs code 会创建 launch.json 文件

image-02

  1. 点击 add configuration 按钮

image-03

  1. 点击 gdb launch 后, vs code会自动补全配置

image-04

补全后如下所示

 1{
 2    // Use IntelliSense to learn about possible attributes.
 3    // Hover to view descriptions of existing attributes.
 4    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
 5    "version": "0.2.0",
 6    "configurations": [
 7        {
 8            "name": "(gdb) Launch",
 9            "type": "cppdbg",
10            "request": "launch",
11            "program": "enter program name, for example ${workspaceFolder}/a.out",
12            "args": [],
13            "stopAtEntry": false,
14            "cwd": "${fileDirname}",
15            "environment": [],
16            "externalConsole": false,
17            "MIMode": "gdb",
18            "setupCommands": [
19                {
20                    "description": "Enable pretty-printing for gdb",
21                    "text": "-enable-pretty-printing",
22                    "ignoreFailures": true
23                },
24                {
25                    "description": "Set Disassembly Flavor to Intel",
26                    "text": "-gdb-set disassembly-flavor intel",
27                    "ignoreFailures": true
28                }
29            ]
30        }
31
32    ]
33}
  1. 在json中设置好程序的路径和参数和程序工作路径
1{
2            "program": "程序路径",
3            "args": ["参数1", "参数2", "..."],
4            "cwd":"程序工作路径"
5}
  1. 点击菜单栏的Run菜单下面的Start Debugging 调试即可

image-05

  1. 使用技巧

🌙