[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

相关推荐
这里有鱼汤12 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
感哥21 小时前
C++ 多态
c++
TF男孩1 天前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
沐怡旸1 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥1 天前
C++ std::set
c++
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法