[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

相关推荐
星空椰1 小时前
从零到实战:一套完整的 Python 爬虫技术体系(requests + BeautifulSoup + 正则 + JSON)
爬虫·python·json·beautifulsoup
AI进化营-智能译站5 小时前
ROS2 C++开发系列17-多线程驱动多传感器|chrono高精度计时实现机器人同步控制
java·c++·ai·机器人
墨染千千秋8 小时前
C++函数的使用以及主函数
c++
七颗糖很甜8 小时前
电离层对地基雷达测量精度的影响分析与校正方法
python
特种加菲猫9 小时前
继承,一场跨越时空的对话
开发语言·c++
AC赳赳老秦9 小时前
知识产权辅助:用 OpenClaw 批量生成专利交底书 / 软著申请材料,自动校验格式与内容合规性
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
WBluuue9 小时前
Codeforces 1093 Div2(ABCD1D2)
c++·算法
小熊Coding9 小时前
Python2D射击冒险闯关游戏2.0版本
python·pygame
FYKJ_201010 小时前
springboot校园兼职平台--附源码02041
java·javascript·spring boot·python·eclipse·django·php
玩转单片机与嵌入式10 小时前
玩转边缘AI(TInyML):需要掌握的C++知识汇总!
开发语言·c++·人工智能