Python调用C/C++的方法解析
Python是解释性语言,底层就是用c实现的,所以用python调用C是很容易的,下面就总结一下各种调用的方法,给出例子,所有例子都在ubuntu9.10,python2.6下试过.
1.Python调用C(base)
想在python中调用c函数,如这儿的fact
#includeintfact(intn) { if(n<=1) return1; else returnn*fact(n-1); } PyObject*wrap_fact(PyObject*self,PyObject*args) { intn,result; if(!PyArg_ParseTuple(args,"i:fact",&n)) returnNULL; result=fact(n); returnPy_BuildValue("i",result); } staticPyMethodDefexampleMethods[]= { {"fact",wrap_fact,METH_VARARGS,"CaculateN!"}, {NULL,NULL} }; voidinitexample() { PyObject*m; m=Py_InitModule("example",exampleMethods); }
把这段代码存为wrapper.c,编成so库,
gcc-fPICwrapper.c-oexample.so-shared -I/usr/include/python2.6-I/usr/lib/python2.6/config
然后在有此so库的目录,进入python,可以如下使用
importexample
example.fact(4)
2.Python调用C++(base)
在python中调用C++类成员函数,如下调用TestFact类中的fact函数,
#includeclassTestFact{ public: TestFact(){}; ~TestFact(){}; intfact(intn); }; intTestFact::fact(intn) { if(n<=1) return1; else returnn*(n-1); } intfact(intn) { TestFactt; returnt.fact(n); } PyObject*wrap_fact(PyObject*self,PyObject*args) { intn,result; if(!PyArg_ParseTuple(args,"i:fact",&n)) returnNULL; result=fact(n); returnPy_BuildValue("i",result); } staticPyMethodDefexampleMethods[]= { {"fact",wrap_fact,METH_VARARGS,"CaculateN!"}, {NULL,NULL} }; extern"C"//不加会导致找不到initexample voidinitexample() { PyObject*m; m=Py_InitModule("example",exampleMethods); }
把这段代码存为wrapper.cpp,编成so库,
g++-fPICwrapper.cpp-oexample.so-shared-I/usr/include/python2.6-I/usr/lib/python2.6/config
然后在有此so库的目录,进入python,可以如下使用
importexample
example.fact(4)
3.Python调用C++(Boost.Python)
Boost库是非常强大的库,其中的python库可以用来封装c++被python调用,功能比较强大,不但可以封装函数还能封装类,类成员.
http://dev.gameres.com/Program/Abstract/Building%20Hybrid%20Systems%20with%20Boost_Python.CHN.by.JERRY.htm
首先在ubuntu下安装boost.python,apt-getinstalllibboost-python-dev
#includecharconst*greet() { return"hello,world"; } BOOST_PYTHON_MODULE(hello) { usingnamespaceboost::python; def("greet",greet); }
把代码存为hello.cpp,编译成so库
g++hello.cpp-ohello.so-shared-I/usr/include/python2.5-I/usr/lib/python2.5/config-lboost_python-gcc42-mt-1_34_1
此处python路径设为你的python路径,并且必须加-lboost_python-gcc42-mt-1_34_1,这个库名不一定是这个,去/user/lib查
然后在有此so库的目录,进入python,可以如下使用
>>>importhello
>>>hello.greet()
'hello,world'
4.python调用c++(ctypes)
ctypesisanadvancedffi(ForeignFunctionInterface)packageforPython2.3andhigher.InPython2.5itisalreadyincluded. ctypesallowstocallfunctionsindlls/sharedlibrariesandhasextensivefacilitiestocreate,accessandmanipulatesimpleandcomplicatedCdatatypesinPython-inotherwords:wraplibrariesinpurePython.ItisevenpossibletoimplementCcallbackfunctionsinpurePython.
http://python.net/crew/theller/ctypes/
#includeclassTestFact{ public: TestFact(){}; ~TestFact(){}; intfact(intn); }; intTestFact::fact(intn) { if(n<=1) return1; else returnn*(n-1); } extern"C" intfact(intn) { TestFactt; returnt.fact(n); }
将代码存为wrapper.cpp不用写python接口封装,直接编译成so库,
g++-fPICwrapper.cpp-oexample.so-shared-I/usr/include/python2.6-I/usr/lib/python2.6/config
进入python,可以如下使用
>>>importctypes
>>>pdll=ctypes.CDLL('/home/ubuntu/tmp/example.so')
>>>pdll.fact(4)
12
到此这篇关于Python调用C/C++的方法解析的文章就介绍到这了,更多相关Python调用C/C++的方法内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。