• Pybinderpython与c++的调用


    目录

    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)

  • 相关阅读:
    【凡尘】---react-redux---【react】
    React生命周期详解
    写文章很难,ai自动生成文章为你来排忧
    怎么用ai智能写作【智媒ai伪原创】快速写文章?
    给大家介绍个Seo伪原创工具吧,可以免费用的哈
    自媒体文章难写,在线伪原创文章生成就简单了
    内容创作难吗 不妨试试智媒ai伪原创
    Ai伪原创工具,轻松几秒出爆文
    什么AI写作软件靠谱,好用?
    分享个免费伪原创工具 关键词自动生成文章
  • 原文地址:https://www.cnblogs.com/lidabo/p/16601613.html
Copyright © 2020-2023  润新知