C + + uses Boost.python to write Python extensions

Source: Internet
Author: User

For a long time did not write the article, today organized some things, here to share.

I've been thinking of using C + + to encapsulate some of the Python extensions that are used in my work, because I haven't written anything like it before in C + +, so I've been looking for some articles on the internet, but I've found that many articles are not very clear, and it should be easy for the veteran, but beginners like me, Will certainly cause a great deal of trouble, because there are always a lot of error, the head is very big, so I will be successful cases to share, and explain in detail the place I have doubts.

Boost.python

Simple description

C + + Write Python Extension module There are many ways, I choose Boost.python to write, feel this is much simpler than the other way, write very little Boost.python code can, so you can focus on writing C + + programs.

Boost.python of science will not need to say, you can Google, I am also Google's own.

Installation

Here are just a few ways to install Ubuntu:

sudo apt-get install libboost-all-dev

Or

sudo apt-get install libboost-python-dev# 这种方式我没有尝试,你们可以自己试试

Source installation is also possible, but need to configure the environment variables, or compile the time is always compiled, the current directly in the compile time to specify the required path is also possible.

Instance

C + + code

The following example makes a description of some vague points.

#include <iostream>#include <string>#include <boost/python.hpp>   // 必须引入这个头文件using namespace boost::python;class HelloWorld{public:    HelloWorld(const std::string& name, int age);    void printInfo();private:    std::string m_name;    int m_age;};HelloWorld::HelloWorld(const std::string& name, int age):m_name(name), m_age(age){}void HelloWorld::printInfo(){    std::cout << "我叫" << m_name << ", " << m_age << "岁了" << std::endl;}void ceshi(){    std::cout << "ceshi" << std::endl;}BOOST_PYTHON_MODULE(helloworld){    // 类导出成Python可调用的动态链接库文件的方式    class_<HelloWorld/* 类名 */, boost::noncopyable /* 单例模式,可有可无 */ >             ("helloworld", init<const std::string&, int/* init里面就是放构造函数的参数,不需要实参 */>())//导出类中的方法            .def("printinfo", &HelloWorld::printInfo);    // 普通函数导出成Python可调用的动态链接库文件的方式    def("ceshi",&ceshi);}

Compiled commands

The following command is my command to compile a dynamic-link library file, modified according to my actual situation.

# python3g++ -shared -o helloworld.so -fPIC -I/usr/include/python3.6m/ helloworld.cpp -lpython3.6m -lboost_python3# python2g++ -shared -o helloworld.so -fPIC -I/usr/include/python2.7/ helloworld.cpp -lpython -lboost_python简单介绍下参数    -shared    // 指定生成动态链接库    -o    // 生成的动态链接库的名称    -fPIC    // 表示使用地址无关代码    -I(大写的i)    // 表示将/usr/include/python2.7/目录作为第一个寻找头文件的目录、    -l    // 指定需连接的库名

No error will be generated in the current directory under the name helloworld.so of a dynamic link library file, directly in the current directory into the Python shell can be tested, of course, you directly into the Python site-packages directory can also be directly in the Python Introduced in the shell environment.

Validation results

>>> import helloworld>>> hw = helloworld.helloworld("lanyulei", 18)>>> hw.printinfo()我叫lanyulei, 18岁了
If there is something wrong with the steps in the article, please point out that I will revise it in the quickest time, thank you.

C + + uses Boost.python to write Python extensions

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.