The Python library is free and open-source. It can be transplanted to multiple operating systems. If it does not depend on the functions of the specified operating system, the Python program can run operations on different operating platforms, this is also one of the advantages that many programmers prefer.
The Boost C ++ Library provides libraries for threads, flexible pointers, operation rules, Python, and more. Many libraries in Boost have been submitted as future C ++ standard attachments. The Boost Python C ++ library is a good template library that allows you to encapsulate any C ++ class or function for Python library users. With this library, it is easier to create a system composed of a C ++ code model and a Python code combination.
First, download and install Boost. Although most of Boost content is included in the header file, the Python library must be compiled. Boost supports most popular compilers on the market, including Visual C ++ 6 and 7. Boost uses a JAM build system. Do not forget to download the JAM library ). For each compiler, you need to set build instructions step by step to build instruction ).
If you follow these instructions, the creation process will be fine. You also need to download and install Python. If you are using a Windows operating system, I strongly recommend that you use Python of ActiveState. After creating Boost, you also need to add the Boost directory to your include path and add the static library and dynamic library to your project.
Compile code in Python. extensions are implemented as DLL or shared libraries, so you need to create a DLL project. In our example project, the Python extension we created will generate a message box, and the text content of the message box can be changed at will.
This example is obviously of little practical value, but it will help you understand the main points of this article. In VC, first create a simple DLL project, and then add a class named CMsgBox to the project. Add a new constructor to this class. The constructor parameter is a standard string. Then, add a Show method to this class. This method has no parameters and its return value is void. The code you get is as follows:
- # Include<String>
-
- Typedefstd: string;
-
- Class CmsgBox
-
- {
-
- String m_Text;
-
- Public:
-
- CMsgBox (void );
-
- Virtual ~ CMsgBox (void );
-
- CMsgBox (const string & text): m_Text (text ){}
-
- Void Show (){
-
- MessageBox (NULL, m_Text.c_str (), "Python", MB_ OK );
-
- }
-
- };
-
- To allow Python to access this class, we need to add the following code:
-
- # Include<Boost/Python/class_builder.hpp>
-
- NamespacePython=Boost: Python;
-
- // Here is the code in the above example
-
- ...
-
- BOOST_PYTHON_MODULE_INIT (PythonDemo)
-
- {
-
- Python: module_builder mod ("PythonDemo ");
-
- Python: class_builder<CMsgBox>Msgbox (mod, "CMsgBox ");
-
- Msgbox. def (python: constructor<String>());
-
- Msgbox. def (CMsgBox: Show, "Show ");
-
- }
This is an executable extension module. The focus here is the BOOST_PYTHON_MODULE_INIT macro. This macro processes module initialization and provides Python with access to classes, constructors, and methods. Note: The DLL name and module name must be the same. After the DLL is created, copy it to The DLL Directory under your Python installation directory. The following describes how to use this extension in Python.
The above Code seems not neat, but you can get other additional gains. You can subclass in the Python Library) This CmsgBox class. If you are not familiar with Python, you generally cannot subclass extension classes in Python .)