[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

相关推荐
CS_浮鱼16 小时前
【Linux】线程
linux·c++·算法
智者知已应修善业16 小时前
【51单片机LED贪吃蛇】2023-3-27
c语言·c++·经验分享·笔记·嵌入式硬件·51单片机
Q264336502316 小时前
【有源码】spark与hadoop-情感挖掘+画像建模的携程酒店评价数据分析可视化系统-基于机器学习的携程酒店评价情感分析与竞争态势可视化
大数据·hadoop·python·机器学习·数据分析·spark·毕业设计
Demon--hx17 小时前
[C++]迭代器
开发语言·c++
倚肆17 小时前
Spring Boot 中的 Bean 与自动装配详解
spring boot·后端·python
不剪发的Tony老师17 小时前
PyScripter:一款免费开源、功能强大的Python开发工具
ide·python
BanyeBirth17 小时前
C++窗口问题
开发语言·c++·算法
郝学胜-神的一滴21 小时前
Qt的QSlider控件详解:从API到样式美化
开发语言·c++·qt·程序人生
橘颂TA21 小时前
【剑斩OFFER】算法的暴力美学——连续数组
c++·算法·leetcode·结构与算法
学困昇1 天前
C++11中的{}与std::initializer_list
开发语言·c++·c++11