boost_python写扩展

2008-1-26 22:18
Boost版本号1.34.1

可以google并参考一篇叫"混合系统接口Boost.Python"的文章
还有
http://wiki.python.org/moin/boost.python/HowTo


http://learn.tsinghua.edu.cn:8080/2005212716/html/boost_python.html


http://visnuhoshimi.spaces.live.com/blog/cns%2135416B2A4DC1B31B%211863.entry



1.bjam python
2.把生成*.lib复制到库目录(可以在djam的输出中看到路径)
3.
进入Boost目录下的“libs\python\build\VisualStudio”子目录中,在VC中打开其中的“boost_python.dsw”文件。
分别编译Boost.Python的Debug和Release版。
编译完成后将在Boost目录下的“libs \python\build\bin-stage”子目录中生成动态链接库和库文件复制出来
4.
    设置好包含文件,库的路径
    有以下内容:
        python的include和lib
        boost的include以及上面生成的lib
       
5.
VC中
    项目->配置属性->常规->配置类型 改为.dll
6.
编译
char const* greet()
{
   return "hello, world";
}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}

将生成的dll改名为hello.pyd
可以复制到python25\dlls下面,也可以在当前目录
结合步骤3生成的dll就可以看到输出了

import hello
print hello.greet()
raw_input()

7.
封装一下,把参数和返回值都封装成python的类型,

#include <boost/python.hpp>
//先包含字典
#include <boost/python/dict.hpp>
class SearchPy{
    ...
    dict search(char* content){
        vector<string,int> result=_search.search(content);
        dict k_c;
        //将原来的返回值封装成Python的字典格式
        for(vec_key_ptr::iterator i=result.begin();i!=result.end();++i)
            k_c.setdefault(i->keyword,i->count);
        return k_c;
    }
};

BOOST_PYTHON_MODULE(search){

   class_<SearchPy>("Search", init<>())
         .def("search", &SearchPy::search)
        //............................
    ;

}

ok,大功告成,可以在python中掉C++了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值