首页 > 编程语言 >Pybinder-python与c++的调用

Pybinder-python与c++的调用

时间:2022-08-19 12:44:47浏览次数:96  
标签: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
From: https://www.cnblogs.com/lidabo/p/16601613.html

相关文章

  • 解决python import找不到自定义包的问题
    文件结构如下├──A│├──a.py│├──__init__.py│└──b.py├──B│├──c.py│├──__init__.py│└──d.py想在c.py调用......
  • C++学生健康信息收集系统
    C++学生健康信息收集系统学生健康信息收集系统简介一、 问题描述为了应对新型冠状病毒疫情,学校需要开发一个能够每天收集全校学生健康信息的系统,便于学校管理。不同学院......
  • Python的数据类型-可变类型和不可变类型
    Python数据类型-可变类型和不可变类型的区别python数据类型有6类:不可变数据类型:数字、字符串、元组可变数据类型:列表、集合、字典可变数据类型和不可变数据类型的区别......
  • Python小整数池-小数据池-驻留机制-is和==区别
    Python小整数池-小数据池-驻留机制-is和==区别1.is和==的区别相同点:都用来比较两个对象是否一样不同点:is用来比较是否是同一个对象,即对象的物理地址是否相同(id(......
  • 10个常用的损失函数解释以及Python代码实现
    什么是损失函数?损失函数是一种衡量模型与数据吻合程度的算法。损失函数测量实际测量值和预测值之间差距的一种方式。损失函数的值越高预测就越错误,损失函数值越低则预测越......
  • 阅读《计算机图形学编程(使用OpenGL和C++)》6
    同一个场景渲染不同的对象,一种简单的方法是为每个模型使用单独的缓冲区。每个模型都需要自己的模型矩阵,这样我们就需要为我们渲染的每个模型生成一个新的模型-视图矩阵。还......
  • Python-05输入输出
    Python输入语句:     在Python3.x中raw_input()和input()进行了整合,去除raw_input(),仅仅保留了Input()函数,其接收任意输入,将所有输入默认为字符串处理,并返回字符......
  • Python - PyPDF2模块的简单使用
    1.简介PyPDF的前身是PyPDF包在2005年发布,该包的最后一个版本发布于2010年,后来大约经过一年左右,名为Phasit的公司赞助PyPDF的一个分支后来命名为PyPDF2,两个版本功能都基本......
  • PYTHON实现倒三角打印
    目录需求数据展示最终结果实现效果代码原始版本1代码效率需求数据展示以空格分隔的990个数据最终结果实现效果代码发现我自己是真的喜欢暴力求解,当然昨天是因为有......
  • python常用函数
    内置函数range()从后往前遍历到0:foriinrange(length-1,-1,-1):先列再行:dp=[[0]*(len2+1)for_inrange(len1+1)]dp=[[0for_inrange(len2+1......