ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Pybinder-python与c++的调用

2022-08-19 13:04:09  阅读:146  来源: 互联网

标签:pybind11 python Pybinder c++ Python library https pybind com


目录

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)

标签:pybind11,python,Pybinder,c++,Python,library,https,pybind,com
来源: https://www.cnblogs.com/lidabo/p/16601613.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有