Using Boost.python to extend Python (python.boost) through the C + + language

Source: Internet
Author: User
Tags file copy

The quality of Python language is not much to say, I would like to mention how to use Boost.python, through the Boost.python can not only transfer Python to C + +, through the Python library, or through C + + to extend Python, The following mainly describes the use of Boost.python to extend the functionality of Python, the first time with Boost.python, Daoteng for half a day to fix:

My test environment is listed first:

I'm using vs2010,python2.7, I created a project with VS2010 for a Windows DLL, the project name is Pylib, and the following code is added to the DLL main:


If the return is not a const char*
You need to add the Python function object definition where the
Boost::p ython::return_value_policy<boost::p ython::return_opaque_pointer> ()
Indicates the type of the return value


char* greet ()
{
Return "Hi extending python!";
}


Boost_python_module (Greet_ext)
{

Since my return value is char*, not const char*, the return value policy must be described or the compilation will not pass.

That's the first question I've ever met.
Boost::p Ython::d EF ("Greet", greet, boost::p ython::return_value_policy<boost::p ython::return_opaque_pointer > ());
}


Then I created a test py file under the compiled release directory to test if the script file was loaded correctly, and my py script file is as follows:

Import Pylib
Print Pylib.greet2 ()


Running the Python datagram DLL load Failed, indicating that the dynamic library failed to load, I checked the original Boost.python of the dynamic library forgot to put in the compiled execution directory. So I'm going to get it from the boost compilation directory

Boost_python-vc100-mt-1_55.dll file copy come on, hey! I did not report the mistake. But to my surprise, there was another problem, saying that I could not find the Initpylib function: (In the midst of my various tossing

) and eventually lock the problem to the name of the module, because Python takes the file name as the module name, so it must be true everywhere, that is, you eventually compile the generated PYD file

(In fact, the dynamic library file, just change to Python can recognize the suffix only) the name must be the name of your export module remains, otherwise Python think the module does not load correctly. So I put

Boost_python_module (Greet_ext)

File name changed to PYD Pylib:

Boost_python_module (Pylib)


Then re-execute the Python test program, OK, the results are displayed correctly:

Below I briefly summarize the Boost.python in the use of the process to pay attention to the problem:

1. Dynamic library compilation output into *.PYD format

2. Make sure that the dynamic library python that the dynamic library relies on can be found, and the simplest is to put them all together.

3. Ensure that the module name specified by the Boost_python_module (module name) is consistent with the file name of the dynamic library

4. Refer to Boost.python for advanced usage such as class.






Using Boost.python to extend Python (python.boost) through the C + + language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.