pybind11 tutorial

pybind11 tutorial

1. Introduction to pybind11

Insert image description here
The GitHub address of the project is: pybind11

pybind11 is a lightweight header library for interoperating between Python and C++. It allows C++ code to be called from Python and vice versa.

Advantages of pybind11 include:

  • Easy to use: The API of pybind11 is simple and easy to understand, even beginners can get started quickly.
  • High performance: pybind11 uses a C++ compiler to generate C extensions for Python, so the performance is very high.
  • Cross-platform: pybind11 supports Windows, Linux and macOS.

Using pybind11 is very simple. Simply include the pybind11 header file in your C++ code and use the API provided by pybind11 to expose C++ types and functions to Python.

2. cmake tutorial on using pybind11

This part of the code is open source at: GitHub pybind- example

cmake_minimum_required(VERSION 3.6)
project(py11_t VERSION 0.1.0 LANGUAGES C CXX)

# 下载pybind11的代码
# 声明 FetchContent 模块
include(FetchContent)

# 声明 pybind11 模块
FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://github.com/pybind/pybind11.git
    GIT_TAG v2.11.1
)

# 使 pybind11 模块可用
FetchContent_MakeAvailable(pybind11)

# 设置pybind11绑定的python版本
set(PYBIND11_PYTHON_VERSION "3.10·")

# pybind11相关的代码将会写入到core文件夹内
add_subdirectory(core)
  • Implement a C++ code, and then wrap it to through pybind11python
#include"fun.hpp"
#include <pybind11/pybind11.h>
namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring
    // 该方法在"fun.hpp"中实现
    m.def("add", &add, "A function that adds two numbers");
    // 该方法在"fun.hpp"中实现
    m.def("sayHi", &sayHi, "say hi");
    // 该方法由lamada表达式形成匿名函数实现
    m.def("hi", [](int i,int j) {return i + j;}, "xxx")
}

After , compile the above code. After compilation, the python corresponding library will be generated in the build folder:
Insert image description here

This library is what python needs. Add the path where the library is located to the system environment variable so that it can be called in python.

import os
import sys
sys.path.append("/Users/chendongsheng/github/py11/build/core")
sys.path.append("/Users/chendongsheng/github/py11/build/")
import cmake_example
cmake_example.sayHi()
print(cmake_example.hi(4,9))
print(cmake_example.add(1,2))


3. History of pybind11

pybind11 was born in 2017 and developed by Wenzel Jakob. Wenzel Jakob is a software engineer from Germany. He has worked at Google for many years and participated in the development of TensorFlow and other projects.

When Wenzel Jakob was developing TensorFlow, he encountered the problem of interoperability between C++ and Python. At that time, he tried various C++ and Python interop libraries, but couldn't find the right one. So, he decided to develop a new C++ and Python interoperability library himself.

Development of pybind11 began in July 2017 and the first version was released in December 2017. The initial version of pybind11 is very simple and only supports interop of C++ basic types and functions.

As pybind11 continues to be developed, it gradually supports more and more C++ features, including:

  • classes and objects
  • template
  • inherit
  • Polymorphism
  • Exception handling
  • Thread safety
  • dynamic type

Development of pybind11 is ongoing, with Wenzel Jakob and other developers continually adding new features and functionality.

pybind11 is a very powerful tool that can be used for a variety of tasks. It can be used to integrate C++ code with Python scripts or to create C++ extensions to Python. It has become the de facto standard in the field of interoperability between C++ and Python.

Guess you like

Origin blog.csdn.net/sexyluna/article/details/134769562