VSCode 的 C/C++ 调试环境的 launch.json、 tasks.json 文件

本文介绍了一种用于C/C++调试的配置方法,通过设置launch.json和tasks.json文件实现自动构建与调试流程。该配置支持使用gdb作为调试器,并能够自动生成与当前编辑文件相关的可执行文件。

launch.json

// Configuring tasks.json for C/C++ debugging
// author: huihut
// repo: https://gist.github.com/huihut/9548fe7e1084cf8e844120c5668b8177

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

tasks.json

// Configuring tasks.json for C/C++ debugging 
// author: huihut
// repo: https://gist.github.com/huihut/887d3c28db92617bd5148c20a5ff112a

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process


{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "g++",
                "args": [
                    "-ggdb",
                    "\"${file}\"",
                    "--std=c++11",
                    "-o",
                    "\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
                ]
            }
        }
    ]
}
### VSCode 中 C/C++ `launch.json` 文件配置教程 在 Visual Studio Code (VSCode) 中开发 C/C++ 项目时,调试功能是一个重要的组成部分。为了实现有效的调试体验,需要正确配置 `.vscode/launch.json` 文件。 #### 配置流程概述 通过快捷键组合 **Ctrl + Shift + P** 调出命令面板并输入 `C/C++` 来启动相关设置向导[^1]。完成基础环境搭建后,在工作区根目录下的 `.vscode` 文件夹中创建或编辑 `launch.json` 文件以支持调试功能。 以下是完整的 `launch.json` 示例及其说明: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", // 调试会话名称 "type": "cppdbg", // 使用 cppdbg 类型进行调试 "request": "launch", // 请求模式为 launch 表示运行新程序 "program": "${workspaceFolder}/a.out", // 可执行文件路径 "args": [], // 命令行参数列表 "stopAtEntry": false, // 是否暂停于入口函数 main() "cwd": "${workspaceFolder}", // 当前工作目录 "environment": [], // 自定义环境变量数组 "externalConsole": true, // 启动外部终端显示输出 "MIMode": "gdb", // 指定调试器为 gdb 或 lldb "setupCommands": [ // 设置 GDB 初始化命令 { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" // 执行预构建任务(需配合 tasks.json) } ] } ``` 上述配置中的每一项都具有特定含义: - `"name"` 定义了该调试配置的友好名称。 - `"type"` 和 `"request"` 明确指定了使用的调试协议以及请求类型。 - `"program"` 是目标可执行文件的位置;通常可以通过 `${command:cmake.launchTargetPath}` 动态获取。 - `"args"` 提供传递给程序的命令行参数。 - `"stopAtEntry"` 控制是否自动停靠到主函数的第一条语句处。 - `"cwd"` 设定当前的工作目录以便加载资源文件或其他依赖。 - `"environment"` 允许指定额外的环境变量集合。 - `"externalConsole"` 决定是否打开独立控制台窗口来展示标准流数据。 - `"MIMode"` 则表明采用哪种底层调试工具链,默认情况下 Linux 平台上使用 GNU Debugger (`gdb`)。 - `"setupCommands"` 添加了一些增强用户体验的功能比如启用漂亮的打印效果。 - `"preLaunchTask"` 关联了一个预先定义的任务用于编译源码之前的操作,这要求存在对应的 `tasks.json` 文件描述具体动作[^3]。 如果希望进一步优化代码样式,则可以调整格式化规则至 Allman 风格而非默认的 K&R 风格: ```json "[c++]": { "editor.defaultFormatter": "ms-vscode.cpptools", "editor.formatOnSave": true, "editor.bracketPairColorization.enabled": true, "[javascript]": {} }, "c_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, AllowShortFunctionsOnASingleLine: None}" ``` 最后需要注意的是,为了避免不必要的干扰建议禁用某些扩展包的提示消息[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值