Pybinder

目录

A really good project.

Export Cpp to python uner Linux platform


A really good project.

https://github.com/pybind/pybind11https://github.com/pybind/pybind11The meaning of this project:

pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.Python library by David Abrahams.

The main issue with Boost.Python—and the reason for creating such a similar project—is Boost. Boost is an enormously large and complex suite of utility libraries that works with almost every C++ compiler in existence. This compatibility has its cost: arcane template tricks and workarounds are necessary to support the oldest and buggiest of compiler specimens. Now that C++11-compatible compilers are widely available, this heavy machinery has become an excessively large and unnecessary dependency.

Think of this library as a tiny self-contained version of Boost.Python with everything stripped away that isn't relevant for binding generation. Without comments, the core header files only require ~4K lines of code and depend on Python (3.6+, or PyPy) and the C++ standard library. This compact implementation was possible thanks to some of the new C++11 language features (specifically: tuples, lambda functions and variadic templates). Since its creation, this library has grown beyond Boost.Python in many ways, leading to dramatically simpler binding code in many common situations.

 I have used in some projects, it's really a large suit of library. Just like pybinder, standalone asio(http://think-async.com/Asio/AsioStandalone) is always used by some people as a clean aiso library from boost.
 

Export Cpp to python under Linux platform

1) A basic example without the help of cmake: 


https://github.com/FrankKuiFang/playGround/tree/main/pybinder/cpp2python/basicExample
编译: 
Execute bash build.sh, this script call c++ complier directly to compile cpp2python.cpp, a .so file will be generated uner the same dir.
执行
Execute python3 test.py,  该python文件需要与编译生成的.so在同一个目录,否则,如下面的基于CMake的例子, 需要设置PYTHONPATH环境变量。  
Note:
该例子官方文档:https://pybind11.readthedocs.io/en/latest/basics.html#creating-bindings-for-a-simple-function
需要先装pybind11: https://pybind11.readthedocs.io/en/latest/installing.html#include-with-pypi
 python3 -m pybind11 --includes指令可以查看pybind pkg的头文件安装目录;
 


2) 在CMake工程中使用pybind11


 https://github.com/FrankKuiFang/playGround/tree/main/pybinder/cpp2python/my_cmake_example
编译:
运行bash build.sh, 该脚本借助Cmake编译, CMakeLists.txt中有两种方式来使用pybind库,都测试ok。  该脚本生成一个.so并安装到install/bin目录;
执行
运行bash run.sh, 该脚本设置PYTHONPATH环境变量,并执行tests/test_basic.py。
Note:
https://pybind11.readthedocs.io/en/latest/compiling.html#find-package-vs-add-subdirectory

CMakeLists.txt中有两种方式来使用pybind库:
1)
下载pybind11源码到你的cpp工程, 例如https://github.com/pybind/cmake_example这个官方的demo就是这个样子的, 其pybind11文件夹下就是https://github.com/pybind/pybind11源码。有了源码,直接使用cmake的add_subdir;根据demo和自己的修改,这里是一个比demo精简的(不用像demo中那样执行setup.py)
 
2)
在系统中install pybind11,然后camake里面就可以使用find_package了。
使用了pip install pybind11这种方式安装,但是cmake中find_packet依然找不到pybind11,因为pip install 这种方式安装后,安装路径需要通过python3 -m pybind11 --includes指令查看,例如 /home/xxx/.local/lib/python3.8/site-packages/pybind11,所以需要在CMakeLists.txt中设置pybind11_ROOT变量来引导cmake发现这个pkg。上面文档里(https://pybind11.readthedocs.io/en/latest/compiling.html#find-package-vs-add-subdirectory)提到的另一种从源码安装的方式,以后可以试试。

Export python to cpp under Linux platform

Embedding the interpreter:

官方文档 https://pybind11.readthedocs.io/en/stable/advanced/embedding.html
借助了py::scoped_interpreter开启一个解释器。该文档对如何些CMakeLists.txt以及如何些cpp做了详细的说明.

(https://github.com/FrankKuiFang/playGround/tree/main/pybinder/python2cpp)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值