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、启动调试
