Boost版本號(hào)1.34.1
可以google并參考一篇叫"混合系統(tǒng)接口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復(fù)制到庫目錄(可以在djam的輸出中看到路徑)
3.
進(jìn)入Boost目錄下的“l(fā)ibs\python\build\VisualStudio”子目錄中,在VC中打開其中的“boost_python.dsw”文件。
分別編譯Boost.Python的Debug和Release版。
編譯完成后將在Boost目錄下的“l(fā)ibs \python\build\bin-stage”子目錄中生成動(dòng)態(tài)鏈接庫和庫文件復(fù)制出來
4.
設(shè)置好包含文件,庫的路徑
有以下內(nèi)容:
python的include和lib
boost的include以及上面生成的lib
5.
VC中
項(xiàng)目->配置屬性->常規(guī)->配置類型 改為.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
可以復(fù)制到python25\dlls下面,也可以在當(dāng)前目錄
結(jié)合步驟3生成的dll就可以看到輸出了
import hello
print hello.greet()
raw_input()
7.
封裝一下,把參數(shù)和返回值都封裝成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++了
聯(lián)系客服