资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python与c函数交互 python c++交互

如何实现C/C++与Python的通信

用C/C++对脚本语言的功能扩展是非常常见的事情,Python也不例外。除了SWIG,市面上还有若干用于Python扩展的工具包,比较知名的还有Boost.Python、SIP等,此外,Cython由于可以直接集成C/C++代码,并方便的生成Python模块,故也可以完成扩展Python的任务。答主在这里选用SWIG的一个重要原因是,它不仅可以用于Python,也可以用于其他语言。如今SWIG已经支持C/C++的好基友Java,主流脚本语言Python、Perl、Ruby、PHP、JavaScript、tcl、Lua,还有Go、C#,以及R。SWIG是基于配置的,也就是说,原则上一套配置改变不同的编译方法就能适用各种语言(当然,这是理想情况了……)SWIG的安装方便,有Windows的预编译包,解压即用,绿色健康。主流Linux通常集成swig的包,也可以下载源代码自己编译,SWIG非常小巧,通常安装不会出什么问题。用SWIG扩展Python,你需要有一个待扩展的C/C++库。这个库有可能是你自己写的,也有可能是某个项目提供的。这里举一个不浮夸的例子:希望在Python中用到SSE4指令集的CRC32指令。首先打开指令集的文档可以看到有6个函数。分析6个函数的原型,其参数和返回值都是简单的整数。于是书写SWIG的配置文件(为了简化起见,未包含2个64位函数):/*File:mymodule.i*/%modulemymodule%{#include"nmmintrin.h"%}int_mm_popcnt_u32(unsignedintv);unsignedint_mm_crc32_u8(unsignedintcrc,unsignedcharv);unsignedint_mm_crc32_u16(unsignedintcrc,unsignedshortv);unsignedint_mm_crc32_u32(unsignedintcrc,unsignedintv);接下来使用SWIG将这个配置文件编译为所谓PythonModuleWrapperswig-pythonmymodule.i得到一个mymodule_wrap.c和一个mymodule.py。把它编译为Python扩展:Windows:cl/LDmymodule_wrap.c/o_mymodule.pyd-IC:\Python27\includeC:\Python27\libs\python27.libLinux:gcc-fPIC-sharedmymodule_wrap.c-o_mymodule.so-I/usr/include/python2.7/-lpython2.7注意输出文件名前面要加一个下划线。现在可以立即在Python下使用这个module了:importmymodulemymodule._mm_popcnt_u32(10)2回顾这个配置文件分为3个部分:定义module名称mymodule,通常,module名称要和文件名保持一致。%{%}包裹的部分是C语言的代码,这段代码会原封不动的复制到mymodule_wrap.c欲导出的函数签名列表。直接从头文件里复制过来即可。还记得本文第2节的那个great_function吗?有了SWIG,事情就会变得如此简单:/*great_module.i*/%modulegreat_module%{intgreat_function(inta){returna+1;}%}intgreat_function(inta);换句话说,SWIG自动完成了诸如Python类型转换、module初始化、导出代码表生成的诸多工作。对于C++,SWIG也可以应对。例如以下代码有C++类的定义://great_class.h#ifndefGREAT_CLASS#defineGREAT_CLASSclassGreat{private:ints;public:voidsetWall(int_s){s=_s;};intgetWall(){returns;};};#endif//GREAT_CLASS对应的SWIG配置文件/*great_class.i*/%modulegreat_class%{#include"great_class.h"%}%include"great_class.h"这里不再重新敲一遍class的定义了,直接使用SWIG的%include指令SWIG编译时要加-c++这个选项,生成的扩展名为cxxswig-c++-pythongreat_class.iWindows下编译:cl/LDgreat_class_wrap.cxx/o_great_class.pyd-IC:\Python27\includeC:\Python27\libs\python27.libLinux,使用C++的编译器g++-fPIC-sharedgreat_class_wrap.cxx-o_great_class.so-I/usr/include/python2.7/-lpython2.7在Python交互模式下测试:importgreat_classc=great_class.Great()c.setWall(5)c.getWall()5也就是说C++的class会直接映射到PythonclassSWIG非常强大,对于Python接口而言,简单类型,甚至指针,都无需人工干涉即可自动转换,而复杂类型,尤其是自定义类型,SWIG提供了typemap供转换。而一旦使用了typemap,配置文件将不再在各个语言当中通用。参考资料:SWIG的官方文档,质量比较高。SWIGUsersManual有个对应的中文版官网,很多年没有更新了。写在最后:由于CPython自身的结构设计合理,使得Python的C/C++扩展非常容易。如果打算快速完成任务,Cython(C/C++调用Python)和SWIG(Python调用C/C++)是很不错的选择。但是,一旦涉及到比较复杂的转换任务,无论是继续使用Cython还是SWIG,仍然需要学习Python源代码。本文使用的开发环境:Python2.7.10Cython0.22SWIG3.0.6Windows10x64RTMCentOS7.1AMD64MacOSX10.10.4文中所述原理与具体环境适用性强。文章所述代码均用于演示,缺乏必备的异常检查

成都创新互联专注于三元网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供三元营销型网站建设,三元网站制作、三元网页设计、三元网站官网定制、成都微信小程序服务,打造三元网络公司原创品牌,更为您提供三元网站排名全网营销落地服务。

python调用c函数

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" //不加会导致找不到initexample

void 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调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.

首先在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) package for Python 2.3 and higher. In Python 2.5 it is already included.

ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.

#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

如何实现 C/C++ 与 Python 的通信

C/C++ 调用 Python(基础篇)

Python 本身就是一个C库。你所看到的可执行体python只不过是个stub。真正的python实体在动态链接库里实现,在Windows平台上,这个文件位于 %SystemRoot%\System32\python27.dll。

你也可以在自己的程序中调用Python,看起来非常容易:

//my_python.c

#include Python.h

int main(int argc, char *argv[])

{

Py_SetProgramName(argv[0]);

Py_Initialize();

PyRun_SimpleString("print 'Hello Python!'\n");

Py_Finalize();

return 0;

}

在Windows平台下,打开Visual Studio命令提示符,编译命令为

cl my_python.c -IC:\Python27\include C:\Python27\libs\python27.lib

在Linux下编译命令为

gcc my_python.c -o my_python -I/usr/include/python2.7/ -lpython2.7

在Mac OS X 下的编译命令同上

产生可执行文件后,直接运行,结果为输出

Hello Python!

Python库函数PyRun_SimpleString可以执行字符串形式的Python代码。

虽然非常简单,但这段代码除了能用C语言动态生成一些Python代码之外,并没有什么用处。我们需要的是C语言的数据结构能够和Python交互。

下面举个例子,比如说,有一天我们用Python写了一个功能特别强大的函数:

def great_function(a):

return a + 1

接下来要把它包装成C语言的函数。我们期待的C语言的对应函数应该是这样的:

int great_function_from_python(int a) {

int res;

// some magic

return res;

}

首先,复用Python模块得做‘import’,这里也不例外。所以我们把great_function放到一个module里,比如说,这个module名字叫 great_module.py

接下来就要用C来调用Python了,完整的代码如下:

#include Python.h

int great_function_from_python(int a) {

int res;

PyObject *pModule,*pFunc;

PyObject *pArgs, *pValue;

/* import */

pModule = PyImport_Import(PyString_FromString("great_module"));

/* great_module.great_function */

pFunc = PyObject_GetAttrString(pModule, "great_function");

/* build args */

pArgs = PyTuple_New(1);

PyTuple_SetItem(pArgs,0, PyInt_FromLong(a));

/* call */

pValue = PyObject_CallObject(pFunc, pArgs);

res = PyInt_AsLong(pValue);

return res;

}

从上述代码可以窥见Python内部运行的方式:

所有Python元素,module、function、tuple、string等等,实际上都是PyObject。C语言里操纵它们,一律使用PyObject *。

Python的类型与C语言类型可以相互转换。Python类型XXX转换为C语言类型YYY要使用PyXXX_AsYYY函数;C类型YYY转换为Python类型XXX要使用PyXXX_FromYYY函数。

也可以创建Python类型的变量,使用PyXXX_New可以创建类型为XXX的变量。

若a是Tuple,则a[i] = b对应于 PyTuple_SetItem(a,i,b),有理由相信还有一个函数PyTuple_GetItem完成取得某一项的值。

不仅Python语言很优雅,Python的库函数API也非常优雅。

现在我们得到了一个C语言的函数了,可以写一个main测试它

#include Python.h

int great_function_from_python(int a);

int main(int argc, char *argv[]) {

Py_Initialize();

printf("%d",great_function_from_python(2));

Py_Finalize();

}

编译的方式就用本节开头使用的方法。

在Linux/Mac OSX运行此示例之前,可能先需要设置环境变量:

bash:

export PYTHONPATH=.:$PYTHONPATH

csh:

setenv PYTHONPATH .:$PYTHONPATH

怎样让Python脚本与C++程序互相调用

二、Python调用C/C++\x0d\x0a\x0d\x0a\x0d\x0a1、Python调用C动态链接库\x0d\x0a\x0d\x0a Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。\x0d\x0a(1)C语言文件:pycall.c\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a/***gcc -o libpycall.so -shared -fPIC pycall.c*/ \x0d\x0a#include \x0d\x0a#include \x0d\x0aint foo(int a, int b) \x0d\x0a{ \x0d\x0a printf("you input %d and %d\n", a, b); \x0d\x0a return a+b; \x0d\x0a} \x0d\x0a(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。\x0d\x0a(3)Python调用动态库的文件:pycall.py\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0aimport ctypes \x0d\x0all = ctypes.cdll.LoadLibrary \x0d\x0alib = ll("./libpycall.so") \x0d\x0alib.foo(1, 3) \x0d\x0aprint '***finish***' \x0d\x0a(4)运行结果:\x0d\x0a\x0d\x0a\x0d\x0a2、Python调用C++(类)动态链接库 \x0d\x0a\x0d\x0a 需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。\x0d\x0a(1)C++类文件:pycallclass.cpp\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a#include \x0d\x0ausing namespace std; \x0d\x0a \x0d\x0aclass TestLib \x0d\x0a{ \x0d\x0a public: \x0d\x0a void display(); \x0d\x0a void display(int a); \x0d\x0a}; \x0d\x0avoid TestLib::display() { \x0d\x0a cout \x0d\x0ausing namespace std; \x0d\x0aint test() \x0d\x0a{ \x0d\x0a int a = 10, b = 5; \x0d\x0a return a+b; \x0d\x0a} \x0d\x0aint main() \x0d\x0a{ \x0d\x0a cout \x0d\x0a#include \x0d\x0a#include \x0d\x0a \x0d\x0aint fac(int n) \x0d\x0a{ \x0d\x0a if (n

回答于 2022-11-16


当前名称:python与c函数交互 python c++交互
URL地址:http://cdkjz.cn/article/dojcdpc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220