Skip to content

附录D: 推荐资源

本文档整理Pybind11学习过程中推荐的优质资源,帮助深入学习和解决疑难问题。

资源链接说明
官方文档https://pybind11.readthedocs.io最权威的学习资料
GitHub仓库https://github.com/pybind/pybind11源码、issue、讨论
官方示例https://github.com/pybind/pybind11/tree/master/tests完整示例代码
CHANGELOGhttps://github.com/pybind/pybind11/blob/master/CHANGELOG.md版本变更历史
教程链接难度
pybind11作者视频https://www.youtube.com/watch?v=jQc3OCOq6gY入门
FOSDEM 2024 pybind11 talkhttps://www.youtube.com/watch?v=M-S8rKiKsKQ进阶
Real Python pybind11教程https://realpython.com/python-bindings-with-pybind11/入门
主题链接
嵌入Python解释器https://pybind11.readthedocs.io/en/latest/advanced/embedding.html
智能指针https://pybind11.readthedocs.io/en/latest/advanced/smart_ptrs.html
STL容器https://pybind11.readthedocs.io/en/latest/advanced/cast/stl.html
NumPy支持https://pybind11.readthedocs.io/en/latest/basics.html#numpy-vector-support

平台链接说明
Stack Overflowhttps://stackoverflow.com/questions/tagged/pybind11搜索[pybind11]标签
GitHub Discussionshttps://github.com/pybind/pybind11/discussions官方讨论区
Discordhttps://discord.gg/xFW7N6mvpybind11官方频道
资源链接说明
pybind11中文文档https://pybind11.readthedocs.io/zh_CN/latest/中文翻译文档
C++社区https://cppquiz.orgC++知识练习
Python官方中文文档https://docs.python.org/zh-cn/3/Python文档
方案链接适用场景
nanobindhttps://nanobind.readthedocs.io轻量级替代,Python 3.12+
Cythonhttps://cython.org/既有C++代码快速绑定
cppyhttps://github.com/nicholasbishop/cppy简化CPython API
SWIGhttp://www.swig.org/多语言绑定

目标:掌握基础绑定,能将简单C++函数导出到Python

  1. 阅读官方文档basics章节
  2. 完成第一个最小化示例
  3. 掌握模块定义、函数导出、基础类型转换
  4. 能够编译和导入模块

练习项目:

  • 将一个C++数学库(如包含加减乘除的类)绑定到Python
  • 实现一个带默认参数的函数

目标:掌握类绑定、智能指针、异常处理

  1. 深入py::class_的各类用法
  2. 掌握运算符重载、继承、虚函数
  3. 理解返回值策略和对象生命周期
  4. 学会处理C++异常和Python异常的相互转换

练习项目:

  • 绑定一个含有成员函数、静态方法的类
  • 实现一个从Python继承C++类并重写虚函数的例子

目标:掌握STL容器、NumPy、迭代器

  1. 理解自动类型转换与opaque类型
  2. 掌握NumPy数组的双向转换
  3. 实现Python可迭代对象
  4. 理解缓冲区协议

练习项目:

  • 将std::vector<std::vector<double>>绑定为Python的嵌套list
  • 实现一个支持NumPy数组输入输出的图像处理函数

目标:掌握GIL、线程、模板、模块组织

  1. 理解GIL机制和释放时机
  2. 掌握多线程下的线程局部存储
  3. 学会使用函数模板和类模板
  4. 能够组织大型多文件模块

练习项目:

  • 实现一个带GIL释放的CPU密集计算函数
  • 组织一个包含多个子模块的大型绑定项目

目标:能够独立完成真实项目,懂得性能调优

  1. 学习性能分析和优化技巧
  2. 掌握调试技术(ASAN、valgrind)
  3. 学习打包分发(wheel、conda)
  4. 了解CI/CD自动化构建

实战项目:

  • 完成一个完整的科学计算库绑定
  • 实现一个机器学习推理模块

项目链接说明
OpenCVhttps://github.com/opencv/opencv计算机视觉库
Eigenhttps://github.com/xyobo/eigen线性代数(部分绑定)
pybind11_protobufhttps://github.com/pybind/pybind11_protobufProtocol Buffers绑定
onnxruntimehttps://github.com/microsoft/onnxruntimeML推理引擎
xtensorhttps://github.com/xtensor-stack/xtensor多维数组表达式
** Dear PyGui**https://github.com/hoffstadt/Dear_PyGuiGUI框架
仓库链接说明
pybind11 exampleshttps://github.com/pybind/pybind11/tree/master/tests官方测试用例
pybind11 starterhttps://github.com/nicholasbishop/pybind11_starter最小化项目模板
scientific-python-bindingshttps://github.com/ThePythonDjangoCom/scientific-python-bindings科学计算示例

书籍说明
C++ PrimerC++入门经典
Effective C++C++最佳实践
Effective Modern C++现代C++最佳实践
The C++ Programming LanguageBjarne Stroustrup著
书籍说明
Fluent PythonPython进阶必读
Python Cookbook实用Python编程
CPython Internals理解Python内部机制
书籍说明
Latency Numbers性能调优参考
Linux性能优化BPF与性能工具

  1. 关注官方更新:pybind11活跃开发,新特性持续加入
  2. 阅读源码:理解内部机制有助于解决疑难问题
  3. 参与社区:GitHub Discussions是获取帮助的好地方
  4. 实践为主:边学边做比纯看书更有效

提示:如果你使用Python 3.12+且项目不需要兼容旧版Python,nanobind是pybind11作者的新项目,更轻量、编译更快、错误信息更好,值得关注。