国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python調用C/C++的方法解析

瀏覽:81日期:2022-07-15 08:23:24

Python是解釋性語言, 底層就是用c實現的, 所以用python調用C是很容易的, 下面就總結一下各種調用的方法, 給出例子, 所有例子都在ubuntu9.10, python2.6下試過.

1. Python 調用 C (base)

想在python中調用c函數, 如這兒的fact

#include <Python.h>int fact(int n){ if (n <= 1) return 1; else return n * fact(n - 1);}PyObject* wrap_fact(PyObject* self, PyObject* args){ int n, result; if (! PyArg_ParseTuple(args, 'i:fact', &n)) return NULL; result = fact(n); return Py_BuildValue('i', result);}static PyMethodDef exampleMethods[] ={ {'fact', wrap_fact, METH_VARARGS, 'Caculate N!'}, {NULL, NULL}};void initexample(){ PyObject* m; m = Py_InitModule('example', exampleMethods);}

把這段代碼存為wrapper.c, 編成so庫,

gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so庫的目錄, 進入python, 可以如下使用

import example

example.fact(4)

2. Python 調用 C++ (base)

在python中調用C++類成員函數, 如下調用TestFact類中的fact函數,

#include <Python.h>class TestFact{ public: TestFact(){}; ~TestFact(){}; int fact(int n);};int TestFact::fact(int n){ if (n <= 1) return 1; else return n * (n - 1);}int fact(int n){ TestFact t; return t.fact(n);}PyObject* wrap_fact(PyObject* self, PyObject* args){ int n, result; if (! PyArg_ParseTuple(args, 'i:fact', &n)) return NULL; result = fact(n); return Py_BuildValue('i', result);}static PyMethodDef exampleMethods[] ={ {'fact', wrap_fact, METH_VARARGS, 'Caculate N!'}, {NULL, NULL}};extern 'C' //不加會導致找不到initexamplevoid initexample(){ PyObject* m; m = Py_InitModule('example', exampleMethods);}

把這段代碼存為wrapper.cpp, 編成so庫,

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so庫的目錄, 進入python, 可以如下使用

import example

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-get install libboost-python-dev

#include <boost/python.hpp>char const* greet(){ return 'hello, world';}BOOST_PYTHON_MODULE(hello){ using namespace boost::python; def('greet', greet);}

把代碼存為hello.cpp, 編譯成so庫

g++ hello.cpp -o hello.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, 可以如下使用

>>> import hello>>> hello.greet()’hello, world’

4. python 調用 c++ (ctypes)

ctypes is an advanced ffi (Foreign Function Interface) packagefor Python 2.3 and higher. In Python 2.5 it is alreadyincluded.

ctypes allows to call functions in dlls/shared libraries and hasextensive facilities to create, access and manipulate simple andcomplicated C data types in Python - in other words: wraplibraries in pure Python. It is even possible to implement Ccallback functions in pure Python.

http://python.net/crew/theller/ctypes/

#include <Python.h>class TestFact{ public: TestFact(){}; ~TestFact(){}; int fact(int n);};int TestFact::fact(int n){ if (n <= 1) return 1; else return n * (n - 1);}extern 'C'int fact(int n){ TestFact t; return t.fact(n);}

將代碼存為wrapper.cpp不用寫python接口封裝, 直接編譯成so庫,

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

進入python, 可以如下使用

>>> import ctypes>>> pdll = ctypes.CDLL(’/home/ubuntu/tmp/example.so’)>>> pdll.fact(4)12

到此這篇關于Python調用C/C++的方法解析的文章就介紹到這了,更多相關Python調用C/C++的方法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久99久久 | 亚洲国产情侣一区二区三区 | 日本韩国欧美在线 | 国产精品亚洲欧美日韩区 | 一级特黄aa大片欧美 | 国产真实乱子伦精品 | 久草在线免费看 | 亚洲综合国产一区在线 | 一本伊大人香蕉高清在线观看 | 日韩一区二区精品久久高清 | 秘书高跟黑色丝袜国产91在线 | 成人影院在线免费观看 | 亚洲一区二区三区久久精品 | 黄人成a动漫片免费网站 | 51久久夜色精品国产 | 国产黄色a三级三级三级 | 国产91丝袜美腿在线观看 | 国产精品国产三级国产an不卡 | 在线一区免费视频播放 | 亚洲欧美中文字幕在线网站 | 久久99精品视频在线在线观看 | 日韩在线视频观看 | 91热久久免费频精品动漫99 | 免费一级欧美片片线观看 | 在线国产视频 | 日韩精品不卡 | 亚洲综合色一区二区三区小说 | 国产视频自拍偷拍 | 一级毛片视频播放 | 99久久国产综合精品1尤物 | 亚洲一区二区三区国产精品 | 国内自拍在线视频高清 | 欧美国产亚洲一区 | 欧美a级在线 | 中文字幕在线不卡 | 中文字幕在线免费观看 | 久久久久久久久免费影院 | 一级黄色香蕉视频 | 久久福利青草精品资源 | 亚州成人| 精品国产一区二区三区在线 |