如何配置 VScode 断点调试Linux 工程代码

1、Windowns 安装WSL 环境

2、VSCode 中 安装 Romote-SSH扩展,进行连接到WSL下的Linux 环境

安装Romote-SSH成功后,在左下角显示 , 点击此图标

出现 "连接到WSL", 进行连接

显示,则表明链接成功

3、 VSCode 安装 C/C++扩展的调试工具

4、 在 linux 中安装gdb工具链

bash 复制代码
sudo apt install gdb

5、在VScode 中配置 tasks.json 和launch.json文件

tasks.json

bash 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "build", // 简化标签名,方便 launch.json 调用
            "command": "/usr/bin/g++-11",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-Wall", // 建议添加:开启所有警告
                "-std=c++11" // 按需指定标准
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true // 设为默认任务
            },
            "detail": "编译器: /usr/bin/g++-11"
        }
    ]
}

lauch.json

bash 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}", // 与 tasks.json 输出路径一致
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "build", // 关联 tasks.json 中的编译任务
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

6、启动调试

相关推荐
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
崔小汤呀4 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应4 天前
vi编辑器使用
linux·后端·操作系统