c++ python交互之boost.p
短信预约 -IT技能 免费直播动态提醒
我们做的项目测试代码用的是python,但是由于开发用的代码是c++,所以很多交互的地方是需要python调用c++的代码。为此我们将c++的函数打包成.so文件被调用。在实际项目中会遇到各种类型的应用,项目组的郭松同学做了比较详细的总结。我将之收集成文,记录如下,便于自己以后翻阅,也为自己博客添砖加瓦,感谢的话就不说了,如果有朋友因为这篇文章受益,请感谢小松同学。
1.普通函数转换
C++代码: class="lazy" data-src.cpp
#include <iostream>
#include <string>
using namespace std;
char const* testA()
{
return "just a test";
}
string testB(string str)
{
cout << str << endl;
return str;
}
void testC(string str,int num)
{
cout << "str is : " << str << endl;
cout << "int is : " << num << endl;
}
python 转换代码: class="lazy" data-src4py.cpp
#include <boost/python.hpp>
#include "class="lazy" data-src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test) //test是python中模块名字
{
def("a", testA);//将testA映射为a 函数
def("b", testB);
def("c", testC);
}
#include "class="lazy" data-src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test) //test是python中模块名字
{
def("a", testA);//将testA映射为a 函数
def("b", testB);
def("c", testC);
}
Makefile文件
因为是第一篇将makefile也展示一下,后面文章将不记录Makefile,因为都一样。/usr/local/boost_143/include/是事先必须准备的库文件。
all:test.so
rm -rf class="lazy" data-src.o class="lazy" data-src4py.o
rm -rf cass_pack_factory_class="lazy" data-src.o cass_pack_Factoryclass="lazy" data-src4py.o
test.so:class="lazy" data-src.o class="lazy" data-src4py.o
g++ class="lazy" data-src4py.o -o test.so -shared -fPIC -I /usr/local/python/include/python2.5/ -I /usr/local/boost_143/include/ -L /usr/local/python/lib/python2.5/ -L
/usr/local/boost_143/lib/ -lboost_python
class="lazy" data-src.o:
g++ -c class="lazy" data-src.cpp -o class="lazy" data-src.o
class="lazy" data-src4py.o:class="lazy" data-src.o
g++ -c class="lazy" data-src4py.cpp -o class="lazy" data-src4py.o -fPIC -I /usr/local/python/include/python2.5/ -I /usr/local/boost_143/include/
clean:
rm -rf class="lazy" data-src.o class="lazy" data-src4py.o
rm -rf test.so
rm -rf class="lazy" data-src.o class="lazy" data-src4py.o
rm -rf cass_pack_factory_class="lazy" data-src.o cass_pack_Factoryclass="lazy" data-src4py.o
test.so:class="lazy" data-src.o class="lazy" data-src4py.o
g++ class="lazy" data-src4py.o -o test.so -shared -fPIC -I /usr/local/python/include/python2.5/ -I /usr/local/boost_143/include/ -L /usr/local/python/lib/python2.5/ -L
/usr/local/boost_143/lib/ -lboost_python
class="lazy" data-src.o:
g++ -c class="lazy" data-src.cpp -o class="lazy" data-src.o
class="lazy" data-src4py.o:class="lazy" data-src.o
g++ -c class="lazy" data-src4py.cpp -o class="lazy" data-src4py.o -fPIC -I /usr/local/python/include/python2.5/ -I /usr/local/boost_143/include/
clean:
rm -rf class="lazy" data-src.o class="lazy" data-src4py.o
rm -rf test.so
python 调用代码:test.py
import test
test.a()
test.b("just a test!")
test.c("call me string!",7)
test.a()
test.b("just a test!")
test.c("call me string!",7)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341