利用C++ Boost编写扩展Python模块

转载自:http://blog.csdn.net/normallife/article/details/52556727 谢谢博主


Python很强大,但已有的模块可能满足不了人民日益增长的物质文化需求,于是有时需要编写扩展模块进行完善。


可行的方案有很多:SWIG、Weave、ctypes、BOOST……

BOOST无疑是开发最快的一种方案。下面介绍下最简单的C++ helloworld程序如何变为Python的一个模块。

1. 安装Python、Boost

这里用Linux环境。Python和Boost都用源码安装,网址为:

Python2.6: https://www.python.org

BOOST1.57.0: http://sourceforge.net/projects/boost/?source=typ_redirect

2. 编写helloworld.cpp

#define BOOST_PYTHON_SOURCE
#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;

void hello_func()
{
        cout<<"hello boost python"<<endl;
}

BOOST_PYTHON_MODULE(boostpy)
{
        def("Hello", hello_func, "Function 's targets...");
}


3. 编译为动态库

命令行中执行:

g++ -shared -o boostpy.so -fPIC -I/YourPythonIncludePath/ helloworld.cpp -lpython2.6 -lboost_python


生成了动态链接库boostpy.so

4. Python环境中调用Hello

>>> import boostpy
>>> boostpy.Hello()
hello boost python
>>>help(boostpy)Help on module boostpy:

NAME
    boostpy

FILE
    /...../boostpy.so

FUNCTIONS
    Hello(...)
        Hello() -> None :
            Function 's targets...

            C++ signature :
                void Hello()
 
就是需要把C++封装成Python可以“理解”的类型。通过使用C++实现测试激励的内部逻辑,然后Python调用C++的这个实现函数即可,这样可以大大减轻脚本编写的速度以及复杂度。
 
 
[python] view plain copy
  1. #include <boost/python.hpp>   
  2. #include <boost/python/module.hpp>   
  3. #include <boost/python/def.hpp>   
  4. #include <boost/python/to_python_converter.hpp>   
  5. #include   
  6. using namespace std;   
  7. using namespace boost::python;  
  8.   
  9. namespace HelloPython{   
  10. // 简单函数   
  11. char const* sayHello(){   
  12.     return "Hello from boost::python";   
  13. }  
  14.   
  15.   
  16. // 简单类   
  17. class HelloClass{   
  18. public:   
  19.     HelloClass(const string& name):name(name){   
  20.     }   
  21. public:   
  22.     string sayHello(){   
  23.       return "Hello from HelloClass by : " + name;   
  24.     }   
  25. private:   
  26.     string name;   
  27. };   
  28. // 接受该类的简单函数   
  29. string sayHelloClass(HelloClass& hello){   
  30.     return hello.sayHello() + " in function sayHelloClass";   
  31. }  
  32.   
  33.   
  34. //STL容器   
  35. typedef vector<int> ivector;  
  36.   
  37.   
  38. //有默认参数值的函数   
  39. void showPerson(string name,int age=30,string nationality="China"){   
  40.     cout << name << " " << age << " " << nationality << endl;   
  41. }  
  42.   
  43. // 封装带有默认参数值的函数   
  44. BOOST_PYTHON_FUNCTION_OVERLOADS(showPerson_overloads,showPerson,1,3) //1:最少参数个数,3最大参数个数  
  45.   
  46. // 封装模块   
  47. BOOST_PYTHON_MODULE(HelloPython){   
  48.     // 封装简单函数   
  49.     def("sayHello",sayHello);  
  50.   
  51.     // 封装简单类,并定义__init__函数   
  52.     class_("HelloClass",init())   
  53.       .def("sayHello",&HelloClass::sayHello)//Add a regular member function   
  54.       ;   
  55.     def("sayHelloClass",sayHelloClass); // sayHelloClass can be made a member of module!!!  
  56.   
  57.     // STL的简单封装方法   
  58.     class_("ivector")   
  59.       .def(vector_indexing_suite());   
  60.     class_ >("ivector_vector")   
  61.       .def(vector_indexing_suite >());  
  62.   
  63.     // 带有默认参数值的封装方法   
  64.     def("showPerson",showPerson,showPerson_overloads());   
  65. }  
转自:
http://www.aichengxu.com/view/2422100
http://www.open-open.com/lib/view/open1329532323890.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值