VSCode Python 与 C++ 联合调试配置指南
为了实现 Python 与 C++ 的联合调试,需要正确配置 launch.json
文件,具体配置如下:
{// IntelliSense 支持查看属性描述// 更多信息请参考: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [// Python 调试配置{"name": "Python 调试程序: 当前文件","type": "debugpy","request": "launch","program": "${file}","console": "integratedTerminal"},// C++ 附加调试配置{"name": "(gdb) 附加到 Python 进程","type": "cppdbg","request": "attach","program": "/usr/bin/python3.8", // 此处需要填写python的绝对路径,如果是虚拟环境,则需要填写虚拟环境的绝对路径"processId": "${command:pickProcess}", // 此处需要填写python的进程id,如果是使用"command:pickProcess",则会跳出一个窗口,选择python的进程id"MIMode": "gdb","setupCommands": [{"description": "启用 gdb 整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "设置 Intel 反汇编风格","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}
获取 Python 进程 ID 的方法
在调试过程中,为了方便识别 Python 进程,可以在 Python 代码中添加以下代码来显示当前进程 ID:
import os# 获取并显示当前进程 ID,便于调试时识别
current_pid = os.getpid()
print(f"当前进程ID: {current_pid}")
使用步骤
- 首先启动 Python 调试会话
- 执行包含进程 ID 显示的 Python 代码
- 启动 C++ 附加调试配置
- 在进程选择器中根据显示的进程 ID 选择对应的 Python 进程