打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
boost_python写扩展

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++了

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
VC6.0下正确配置boost库使用正则表达式(总编)
动态链接 boost 库,犯了个2B错误
(原创)compile cortex-vfx on Ubuntu10.04
boost 1.45.0 的安装及测试(Visual Studio 2010)
VC 中使用MATLAB的C 数学库和MCC生成的程序
很好的c 和Python混合编程文章
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服