Python调用C

Python调用C

关键字: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");
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

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

import ctypes 
testlib = ctypes.CDLL('/path/to/testlib.so')
testlib.myprint()
 
 
  • 1
  • 2
  • 3

第四步: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;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第二步:将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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第四步: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;
}
 
 
  • 1
  • 2
  • 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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

第四步:just run it

$ python test2.py 
result is: 17

范例四 参数为结构体

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

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;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第二步:将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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

第四步:just run it

$ python test3.py 
6 10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值