您的位置:首页 > 编程语言 > Python开发

【转】Python调用C函数

2015-08-19 15:46 756 查看
原文链接:

Python调用C函数 – 快课网 http://www.cricode.com/359.html
关键字:Python ctypes,Python调用dll,Python调用C函数
为了节省软件开发成本,软件开发人员希望能够缩短的软件的开 发时间,希望能够在短时间内开发出稳定的产品。Python 功能强大,简单易用,能够快速开发应用软件。但是由于 Python 自身执行速度的局限性,对性能要求比较高的模块需要使用效率更高的程序语言进行开发,例如 C 语言,系统的其他模块运用 Python 进行快速开发,最后将 C 语言开发的模块与 Python 开发的模块进行整合。在此背景下,基于 Python 语言与 C 语言的各自特点,用 C 语言来扩展现有的 Python 程序,显得很有意义。
本文提供Python以链接库的形式调用C函数的实例。主要用到Python提供的ctypes实现。Python官方教程地址:Ctypes

范例一:Python 调用 C 语言 so
第一步:编写C函数,testlib.c

#include <stdio.h>
void myprint()
{
  printf("hello,www.cricode.com!n");
}
第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib -o testlib.so -fPIC testlib.c
如果在Mac OS X ,则
$ gcc -shared -Wl,-install_name,testlib.so -o testlib.so -fPIC testlib.c
第三步:在python中使用C链接库函数,编写python代码test.py如下

import ctypes

testlib = ctypes.CDLL('/path/to/testlib.so')
testlib.myprint()

第四步:just run it

$ python test.py
hello,www.cricode.com!

范例二:参数类型为字符串(即传指针)
当C函数中,参数类型为字符串时,使用ctypes提供的create_string_buffer来创建缓存区。

第一步:编写C函数testlib1.c

#include <string.h>
int reverse(char* str)
{
int i,j,t;
for(i=0,j=strlen(str) - 1;i<j;i++,j--){
t = str[i];
str[i] = str[j];
str[j] = t;
}
return 0;
}
第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib1 -o testlib1.so -fPIC testlib1.c

第三步:在python中使用C链接库函数,编写python代码test1.py如下:

import ctypes

s0 = 'hello,www.cricode.com'
s1 = ctypes.create_string_buffer(s0)
testlib1 = ctypes.CDLL('./testlib1.so')
testlib1.reverse(s0)
print 's0 is: ',s0
print 's1 is: ',s1.value
第四步:just run it

$ python test1.py
s0 is: moc.edocirc.www,olleh
s1 is: hello,www.cricode.com

范例三:参数为指针、数组(传指针、数组)
第一步:编写C函数testlib2.c

void arrayFunc(int *sum,int arr[4])
{
*sum = arr[0]+ arr[1]*2 + arr[2]*3 + arr[3]*4;
}
第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib2 -o testlib2.so -fPIC testlib2.c

第三步:在python中使用C链接库函数,

import ctypes

result = ctypes.c_int()
array = ctypes.c_int*4
parray = array(ctypes.c_int(1),ctypes.c_int(2),ctypes.c_int(3),ctypes.c_int(4))

testlib2 = ctypes.CDLL('./testlib2.so')
#testlib2.arrayFunc.argtypes=[ctypes.c_void_p,ctypes.c_int*4]
testlib2.arrayFunc(ctypes.pointer(result),parray)
print 'result is:',result.value
第四步:just run it

$ python test2.py
result is: 17

范例四 参数为结构体
第一步:编写C函数testlib3.c

编写python代码test2.py如下:

typedef struct{
int x;
int y;
}mystruct;
mystruct structFunc(mystruct a,mystruct b)
{
mystruct s;
s.x = a.x + b.x;
s.y = a.y + b.y;
return s;
}

第二步:将C函数编译成链接库

$ gcc -shared -Wl,-soname,testlib3 -o testlib3.so -fPIC testlib3.c

第三步:在python中使用C链接库函数,编写python代码test3.py如下:

import ctypes

class mystruct(ctypes.Structure):
_fields_ = [("x",ctypes.c_int),
("y",ctypes.c_int)]

a = mystruct(2,4)
b = mystruct(4,6)
testlib3 = ctypes.CDLL('./testlib3.so')
#srestype muste be setted to avoid segment fault
testlib3.structFunc.restype = mystruct
c = testlib3.structFunc(a,b)
print c.x,c.y
第四步:just run it

$ python test3.py
6 10
范例五 回调函数的使用
回调函数是什么?

回调函数是由你自己定义,但你永远也不会调用它的一类函数的总称。

【也就是说,你是活雷锋,你在给别人做嫁衣】

continue….
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: