栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

VSCode Python/C++ 混合Debug

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

VSCode Python/C++ 混合Debug

VSCode Python/C++ 混合Debug
  • 修改 set_up.py将c++编译为debug info模式
  • 配置launch.json
  • 拿到pid
  • Troubleshoot attaching to processes using GDB
  • 开始debug
  • docker中使用这套流程

修改 set_up.py将c++编译为debug info模式

主要是修改setup.py中ext_modules模块
分别在extra_compile_args和extra_link_args中添加’-g’,此处表示为debug模式,如果此时编译,后面进入c++代码后会不停地跳,原因是因为c++编译的时候会自动优化,所以还需要gcc编译的时候不让它优化,因此还需要添加’-O0’,这样就可以正确debug c or c++代码了。
修改之后代码为

ext_modules = [
    Pybind11Extension("sampler_core", 
                      ['sampler_core.cpp'],
                      extra_compile_args = ['-fopenmp','-g','-O0'],
                      extra_link_args = ['-fopenmp','-g'],),
]

然后重新编译一下即可

配置launch.json

先在vscode上安装插件C/C++ Extension Pack,然后把launch.json修改如下

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/home/amax/anaconda3/envs/torch/bin/python",   //-- 修改这一栏为你执行pytorch的python路径
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false
        }
    ]
}

拿到pid

在python调用C++的代码处加断点(当然也可以使用pdb在代码里pdb.set_trace()),使用

  • os.getpid()
  • ps -ef | grep python
Troubleshoot attaching to processes using GDB

使用网页中第一种方式解决

开始debug
  • vscode c++ debug vector显示不全的解决方案
  • 最简单的方法:*(type(*)[size])array_name
  • 其他方法还有打开pretty-printer的方式,看上去需要配置,没去尝试了
docker中使用这套流程

还没尝试,zz同学应该试成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982849.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号