Boost版本号1.34.1
可以google并参考一篇叫"混合系统接口Boost.Python"的文章 还有 http://wiki./moin/boost.python/HowTo http://learn.:8080/2005212716/html/boost_python.html
http://visnuhoshimi.spaces./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++了
|