[pybind11] debug C++代码

首先要有一个项目,我发布在github上了【传送门

项目的结构如下:

其中src目录下是C++代码,test.py是python测试代码。

然后直接开始演示。

1、把项目下载到本地

bash 复制代码
git clone --recursive https://github.com/immortalmin/pybind11_debug_eg.git

2、修改CMakeLists.txt(包括当前项目和pybind11),各添加两行代码

当前项目:

python 复制代码
# pybind_debug_eg/CMakeLists.txt
cmake_minimum_required(VERSION 3.4...3.18)
project(cmake_example)

# ------- 添加这两行 -------
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")

add_library(mymath STATIC src/mymath.cpp)
set_target_properties(mymath PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_subdirectory(pybind11)
pybind11_add_module(cmake_example src/binder.cpp)
target_link_libraries(cmake_example PRIVATE mymath)

pybind11:

python 复制代码
# pybind11_debug_eg/pybind11/CMakeLists.txt
...
cmake_minimum_required(VERSION 3.5)

# ------- 添加这两行 -------
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
  cmake_policy(VERSION 3.26)
endif()
...

3、编译项目

bash 复制代码
# 1.先切换回项目的根目录
# 2.
mkdir build; cd build
# 3.-DPYTHON_EXECUTABLE是python的路径
cmake .. -DPYTHON_EXECUTABLE=/Users/username/miniconda3/bin/python
# 4.
cd ..
python setup.py build_ext --inplace
# 然后就生成了一个.so文件

4、测试编译是否正常

bash 复制代码
# 在项目根目录执行
python test.py

# 输出结果:
# i: 1, j: 2
# 3

5、debug

bash 复制代码
# 1.运行ipython。没安装的直接运行 pip install ipython
ipython
# 2.进入ipython后,运行下面的命令,找到ipython的pid。假设为11003
In [1]: !ps aux | grep -i ipython

另外打开一个终端

bash 复制代码
# 1.运行lldb
lldb
# 2.进入lldb后
(lldb) attach --pid 11003
# 3.
(lldb) c
# 4.在想要debug的地方打上断点
(lldb) breakpoint set -f mymath.cpp -l 5

返回第一个终端

bash 复制代码
# 执行test.py
run test.py

你会发现卡住了,这时候第二个终端就会显示debug的画面

参考博客与视频:
Debugging C/C++ libraries called by Python
Using C++ in python with pybind11 and cmake

相关推荐
m0_7155753428 分钟前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
甄心爱学习30 分钟前
【leetcode】判断平衡二叉树
python·算法·leetcode
深蓝电商API34 分钟前
滑块验证码破解思路与常见绕过方法
爬虫·python
阿猿收手吧!34 分钟前
【C++】string_view:高效字符串处理指南
开发语言·c++
Ulyanov36 分钟前
Pymunk物理引擎深度解析:从入门到实战的2D物理模拟全攻略
python·游戏开发·pygame·物理引擎·pymunk
sensen_kiss1 小时前
INT303 Coursework1 爬取影视网站数据(如何爬虫网站数据)
爬虫·python·学习
玄同7651 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
Word码1 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
Yorlen_Zhang1 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl13071 小时前
C++算法(1)双指针
开发语言·c++