如何在python中调用C语言代码

您所在的位置:网站首页 python里import函数 如何在python中调用C语言代码

如何在python中调用C语言代码

2023-12-08 02:49| 来源: 网络整理| 查看: 265

1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码

开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/C API。每种方式也都有各自的利弊。

首先,我们要明确为什么要在Python中调用C?

常见原因如下: - 你要提升代码的运行速度,而且你知道C要比Python快50倍以上 - C语言中有很多传统类库,而且有些正是你想要的,但你又不想用Python去重写它们 - 想对从内存到文件接口这样的底层资源进行访问 - 不需要理由,就是想这样做

2.CTypesPython中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正是如此奠定了这种方法的简单性。

示例如下

实现两数求和的C代码,保存为add.c

//sample C file to add 2 numbers - int and floats

#include int add_int(int, int); float add_float(float, float); int add_int(int num1, int num2){ return num1 + num2; } float add_float(float num1, float num2){ return num1 + num2; }

 

接下来将C文件编译为.so文件(windows下为DLL)。下面操作会生成adder.so文件

#For Linux $ gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c #For Mac $ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c

 

现在在你的Python代码中来调用它

from ctypes import * #load the shared object file adder = CDLL('./adder.so') #Find sum of integers res_int = adder.add_int(4,5) print "Sum of 4 and 5 = " + str(res_int) #Find sum of floats a = c_float(5.5) b = c_float(4.1) add_float = adder.add_float add_float.restype = c_float print "Sum of 5.5 and 4.1 = ", str(add_float(a, b))

 

输出如下

Sum of 4 and 5 = 9Sum of 5.5 and 4.1 = 9.60000038147在这个例子中,C文件是自解释的,它包含两个函数,分别实现了整形求和和浮点型求和。

在Python文件中,一开始先导入ctypes模块,然后使用CDLL函数来加载我们创建的库文件。这样我们就可以通过变量adder来使用C类库中的函数了。当adder.add_int()被调用时,内部将发起一个对C函数add_int的调用。ctypes接口允许我们在调用C函数时使用原生Python中默认的字符串型和整型。

而对于其他类似布尔型和浮点型这样的类型,必须要使用正确的ctype类型才可以。如向adder.add_float()函数传参时, 我们要先将Python中的十进制值转化为c_float类型,然后才能传送给C函数。这种方法虽然简单,清晰,但是却很受限。例如,并不能在C中对对象进行操作。

3.SWIGSWIG是Simplified Wrapper and Interface Generator的缩写。是Python中调用C代码的另一种方法。在这个方法中,开发人员必须编写一个额外的接口文件来作为SWIG(终端工具)的入口。

Python开发者一般不会采用这种方法,因为大多数情况它会带来不必要的复杂。而当你有一个C/C++代码库需要被多种语言调用时,这将是个非常不错的选择。

示例如下(来自SWIG官网)

example.c文件中的C代码包含了不同的变量和函数

#include double My_variable = 3.0; int fact(int n) { if (n >> import example>>> example.fact(5)120>>> example.my_mod(7,3)1>>> example.get_time()'Sun Feb 11 23:01:07 1996'>>>我们可以看到,使用SWIG确实达到了同样的效果,虽然下了更多的工夫,但如果你的目标是多语言还是很值得的。

4.Python/C APIPython/C API可能是被最广泛使用的方法。它不仅简单,而且可以在C代码中操作你的Python对象。

这种方法需要以特定的方式来编写C代码以供Python去调用它。所有的Python对象都被表示为一种叫做PyObject的结构体,并且Python.h头文件中提供了各种操作它的函数。例如,如果PyObject表示为PyListType(列表类型)时,那么我们便可以使用PyList_Size()函数来获取该结构的长度,类似Python中的len(list)函数。大部分对Python原生对象的基础函数和操作在Python.h头文件中都能找到。

示例

编写一个C扩展,添加所有元素到一个Python列表(所有元素都是数字)

来看一下我们要实现的效果,这里演示了用Python调用C扩展的代码

#Though it looks like an ordinary python import, the addList module is implemented in Cimport addList

l = [1,2,3,4,5]print "Sum of List - " + str(l) + " = " + str(addList.add(l))上面的代码和普通的Python文件并没有什么分别,导入并使用了另一个叫做addList的Python模块。唯一差别就是这个模块并不是用Python编写的,而是C。

接下来我们看看如何用C编写addList模块,这可能看起来有点让人难以接受,但是一旦你了解了这之中的各种组成,你就可以一往无前了。

//Python.h has all the required function definitions to manipulate the Python objects

#include //This is the function that is called from your python code static PyObject* addList_add(PyObject* self, PyObject* args){ PyObject * listObj; //The input arguments come as a tuple, we parse the args to get the various variables //In this case it's only one list variable, which will now be referenced by listObj if (! PyArg_ParseTuple( args, "O", &listObj )) return NULL; //length of the list long length = PyList_Size(listObj); //iterate over all the elements int i, sum =0; for (i = 0; i < length; i++) { //get an element out of the list - the element is also a python objects PyObject* temp = PyList_GetItem(listObj, i); //we know that object represents an integer - so convert it into C long long elem = PyInt_AsLong(temp); sum += elem; } //value returned back to python code - another python object //build value here converts the C long to a python integer return Py_BuildValue("i", sum); } //This is the docstring that corresponds to our 'add' function. static char addList_docs[] = "add( ): add all elements of the list\n"; /* This table contains the relavent info mapping - , , , */ static PyMethodDef addList_funcs[] = { {"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs}, {NULL, NULL, 0, NULL} }; /* addList is the module name, and this is the initialization block of the module. , ,


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3