在 VSCode 上配置使用 GDB
手动版本
- 在文件目录下新建文件夹
.vscode
- 在
.vscode
中创建文件lunch.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": "gcc - 生成和调试活动文件",
- "type": "cppdbg",
- "request": "launch",
- "program": "${fileDirname}/${fileBasenameNoExtension}",
- "args": [],
- "stopAtEntry": false,
- "cwd": "${fileDirname}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "gdb",
- "setupCommands": [
- {
- "description": "为 gdb 启用整齐打印",
- "text": "-enable-pretty-printing",
- "ignoreFailures": true
- }
- ],
- "preLaunchTask": "C/C++: gcc 生成活动文件",
- "miDebuggerPath": "/usr/bin/gdb"
- }
- ]
- }
其中,program
是需要运行的程序的路径,${fileDirname}
和 ${fileBasenameNoExtension}
是特殊变量。
args
是需要传入程序的启动参数,是一个 list,每一个 item 都是一个不包含空格的字符串。
3. 如果想要在调试前自动编译,在.vscode
中创建文件 task.json
。并粘贴如下配置。
- {
- "tasks": [
- {
- "type": "cppbuild",
- "label": "C/C++: gcc 生成活动文件",
- "command": "/usr/bin/gcc",
- "args": [
- "-fdiagnostics-color=always",
- "-g",
- "${file}",
- "-o",
- "${fileDirname}/${fileBasenameNoExtension}"
- ],
- "options": {
- "cwd": "${fileDirname}"
- },
- "problemMatcher": [
- "$gcc"
- ],
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "detail": "调试器生成的任务。"
- }
- ],
- "version": "2.0.0"
- }
按需修改配置就好了。
4. 运行调试
自动版本
就会自动创建文件,然后在文件里按需配置就好了。