Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call C++ by Python to make Python Interface through Pybind11

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article introduces in detail "how Python calls C++ to make Python interface through Pybind11" with detailed content, clear steps and proper handling of details. I hope that this article "how to call C++ through Pybind11 to make Python interface by Python" can help you solve your doubts.

1. Brief introduction of pybind11 and environment installation

Pybind11 is a lightweight library containing only header files, it is mainly used to expand on the basis of the existing C++ code, its syntax and goals are very similar to Boost.Python, but Boost.Python in order to be compatible with all the existing C++ compilers has become very complex and huge, at the expense of a lot of obscure template techniques and a lot of unnecessary support for older compilers. Pybind11 abandons this support and only supports compilers above python2.7 and C++ 11, making it more concise and efficient than Boost.Python.

In order to use pybind11, we need compilers that support the C++ 11 standard (GCC 4.8or above, VS 2015 Update 3 or above) and python 2.7.We also need to download CMake.

First, we download the source code from the pybind11 github URL.

Pytest pip install pytest must be installed before cmake project, otherwise errors will occur.

Compile and run the test case with CMake:

Enter the pybind11 directory, cd testscmake.. cmake-- build. -config Release-target check

If all the test cases pass, the installation is successful.

2. Python calls C++

After downloading and compiling pybind11, we can start to learn from the official pybind11 Tutorial. For detailed introductory tutorials and grammar, please refer to the official documentation. Here, we briefly demonstrate how to write a C++ module for python call.

First, we write a C++ source file named example.cpp

# include namespace py = pybind11;int add (int I, int j) {return I + j;} PYBIND11_MODULE (example, m) {/ / optional module docstring m.doc () = "pybind11 example plugin"; / / expose add function, and add keyword arguments and default arguments m.def ("add", & add, "A function which adds two numbers", py::arg ("I") = 1, py::arg ("j") = 2); / / exporting variables m.attr ("the_answer") = 42 Py::object world = py::cast ("World"); m.attr ("what") = world;}

2.1 compile with window

I don't have an experiment, so I can refer to other tutorials

2.2 compilation method of CMake

Of course, we can also compile using CMake. First, write a CMakeLists.txt.

Cmake_minimum_required (VERSION 2.8.12) project (example) add_subdirectory (pybind11) pybind11_add_module (example example.cpp)

Here, example.cpp is required to be placed in the same directory as pybind11, because we call the pybind11 and example.cpp files in the same directory in CMakeLists.txt. Execute under the current directory

Cmake .make

An example.cpython-36m-x86_64-linux-gnu.so file is generated. This file is the file that python can call. Or run python in the same directory and enter the python command line

Import exampleexample.add (3,4) [out]: 7

3. Intermediate call

The above is a simple example, and sometimes the functionality we need can be complex.

Generate the model design library invocation problem.

For example, if you reference other third-party libraries in your cpp file, the so file we generate at this time may need to rely on third-party libraries.

Local myopencv.cpp file

# include # include namespace py = pybind11;void read_img (std::string img_path) {cv::Mat image = cv::imread (img_path, CV_LOAD_IMAGE_COLOR);} PYBIND11_MODULE (myopencv, m) {m.def ("read_img", & read_img, "get image size");}

CMakeLists.txt can be written as follows

Cmake_minimum_required (VERSION 2.8.12) project (myopencv) add_subdirectory (pybind11) pybind11_add_module (myopencv myopencv.cpp)

Compiled through cmkae

Cmake .make

Generate myopencv.cpython-36m-x86_64-linux-gnu.so files

Call in python

Import myopencv has read this, the article "how Python calls C++ to make Python interface through Pybind11" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report