The boost. Python library provides a convenient way to import C++ functions, classes, and so on into python for use, and we only need to add a small amount of code to integrate C++ with python.
After compiling C++ code into a python module with the help of boost. Python, we can import the module as we would a standard module, using C++ classes in python to create variables and call its methods. In short, we can easily call C++ functions in Python, and the combination of the two improves both the efficiency of the program and the efficiency of programming.
The steps to install and use the Boost. Python library are as follows.
Development environment: Windows, Visual Studio 2015 version: boost. Python 1.64.0 download address: www.boost.org/users/histo…
Build boost. Python libraries
1. Unzip the downloaded Boost. Python package to drive D to get D:\boost_1_64_0
2. Run the bootstrap.bat batch file in the directory and wait for a while. Create a configuration file for static library compilation as follows:
# Configure specific Python version. using Python: 2.7: C:/Python27/python.exe: C:/Python27/include #directory that contains pyconfig.h : C:/Python27/libs #directory that contains python27.lib : <toolset>msvc ;Copy the code
Python2.7 is used here. You need to change C:/Python27 to the Python installation path on your computer. MSVC means to use the Visual Studio toolkit.
3. Run the CD command to switch the working directory to D:\boost_1_64_0 and run the following command:
b2 toolset=msvc14.0 --with-python variant=debug runtime-debugging=on link=static --user-config=user-config.jam stageCopy the code
This command will generate the required static library file based on the configuration file above.
Second, VS engineering configuration
Open Project Properties -> Configuration Properties -> C/C++ in Visual Studio
Add D:\ BOOST_1_64_0 in the “Additional Include Directory” on the right; C:\ python27\ include, these two directories hold the required header files.
Go to Project Properties -> Configure Properties -> Linker -> General
Add C:\Python27\libs in the “Additional Library directory” on the right; D: boost_1_64_0 stage lib, these directories store library files needed for the linking stage.
Add C++ code
After compiling the boost. Python library and setting up the VS project, you can wrap the C++ code.
First, we need to add a macro definition to our C++ code that tells VS that we are using the compiled boost. Python static link library. This macro definition needs to precede all code (including #include statements).
#define BOOST_PYTHON_STATIC_LIBCopy the code
For example, here’s a C++ function:
char const* greet(a)
{
return "hello, world";
}Copy the code
We need to add the following code:
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello_ext)
{
def("greet", greet);
}Copy the code
Note that hello_ext in the code is the name of the target Python module, which must correspond to the file name.
This code can be placed in a separate source file, associated with #include, or added directly to existing source code. If the source code has been compiled to a.lib file, in this case the interface can be written in a separate source file.
4. Generate DLL files
Open project Properties -> Config Properties -> General and select dynamic Library (.dll) from the project defaults on the right -> Config Type. This will cause VS to generate DLL dynamic library files instead of exe executables.
Go to Build -> Build solutions and generate a.dll file. Copy the file to the Python project directory and change the suffix to.pyd to get the hello_ext.pyd file, which is a Python library file that can be imported directly.
5. Call from Python
We can call our own generated modules as if they were standard Python libraries:
>>> import hello_ext
>>> print hello_ext.greet()
hello, worldCopy the code
Attached: class wrapper
The example given above is an example of a function wrapper. If you are exporting a C++ class, you need to use the class wrapper to wrap the interface.
For example, we want to export the following C++ class to Python to use:
class World
{
void set(std: :string msg) { this->msg = msg; }
std: :string greet(a) { return msg; }
std: :string msg;
};Copy the code
We need to add the following wrapper to the original code to provide the interface to Python:
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set); }Copy the code
Then follow the steps above to generate the.pyd module file, which is called in Python:
>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'Copy the code
conclusion
The Boost. Python library is also very handy, and normally allows us to export data structures and functions into Python without changing the original code. Not only is C++ and python integration more efficient than using the python.h library yourself. Most surprising to me is that the library also provides data types such as boost::python::list, which we can add to a list directly in C++ the way we use lists in python. We can also use the list type to easily pass multiple data to Python as lists.
Of the above steps, the most likely to have problems is the “generate DLL file” step, there may be library files can not be found, or symbols load failure and so on. If you encounter these problems, check that the Boost. Python library is compiled correctly, that the correct header file path and additional library path are added to the project properties, that the installed Python version has the same number of bits as the VS configuration, and so on. If you can’t solve the problem, you can search for the solution by Google. StackOverflow has many solutions for these problems. Feel free to leave a comment for me to discuss the problem.
For more instructions on how to use Boost.Python, see Boost.Python tutorial-1.64.0
Copyright statement
The original author: Wray, Zheng links: www.codebelief.com/article/201…