pythonc函数_Python调用C函数

范例一:Python 调用 C 语言 so

第一步:编写C函数,testlib.c

1

2

3

4

5

#include

voidmyprint()

{

printf("hello,www.cricode.com!n");

}

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

1

2

3

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

如果在Mac OSX,则

$gcc-shared-Wl,-install_name,testlib.so-otestlib.so-fPIC testlib.c

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

1

2

3

4

import ctypes

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

testlib.myprint()

第四步:just run it

1

2

$python test.py

hello,www.cricode.com!

范例二:参数类型为字符串(即传指针)

当C函数中,参数类型为字符串时,使用ctypes提供的create_string_buffer来创建缓存区。

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

1

2

3

4

5

6

7

8

9

10

11

#include

intreverse(char*str)

{

inti,j,t;

for(i=0,j=strlen(str)-1;i

t=str[i];

str[i]=str[j];

str[j]=t;

}

return0;

}

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

1

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

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

1

2

3

4

5

6

7

8

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

1

2

3

$python test1.py

s0 is:moc.edocirc.www,olleh

s1 is:hello,www.cricode.com

范例三:参数为指针、数组(传指针、数组)

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

1

2

3

4

voidarrayFunc(int*sum,intarr[4])

{

*sum=arr[0]+arr[1]*2+arr[2]*3+arr[3]*4;

}

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

1

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

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

1

2

3

4

5

6

7

8

9

10

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

1

2

$python test2.py

result is:17

范例四 参数为结构体

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

编写python代码test2.py如下:

1

2

3

4

5

6

7

8

9

10

11

typedefstruct{

intx;

inty;

}mystruct;

mystruct structFunc(mystructa,mystructb)

{

mystructs;

s.x=a.x+b.x;

s.y=a.y+b.y;

returns;

}

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

1

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

import ctypes

classmystruct(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)

printc.x,c.y

第四步:just run it

1

2

$python test3.py

610

范例五 回调函数的使用

回调函数是什么?

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

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

continue….

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值