python 怎么调用c语言接口

Python014

python 怎么调用c语言接口,第1张

ctypes:  可直接调用c语言动态链接库。

使用步骤:

1>编译好自己的动态连接库

2>利用ctypes载入动态连接库

3>用ctype调用C函数接口时,需要将python变量类型做转换后才能作为函数参数,转换原则见下图:

4>Python若想获取ctypes调用的C函数返回值,需要先指定返回值类型。我们将在接下来的完整Sample中看到如何使用。

#Step 1:  test.c#include <stdio.h>

int add(int a, int b)

{

    return a + b

}#Step 2: 编译动态链接库 ( 如何编译动态链接库在本文不详解,网上资料一大堆。)gcc -fPIC -shared test.c -o libtest.so  

#Step 3:  test.py

from ctypes import *mylib = CDLL("libtest.so")   或者   cdll.LoadLibrary("libtest.so")   add = mylib.add

add.argtypes = [c_int, c_int] # 参数类型,两个int(c_int是ctypes类型,见上表)

add.restype = c_int # 返回值类型,int (c_int 是ctypes类型,见上表)

sum = add(3, 6)

可以的。

C中内嵌Python

新建立一个工程,首先需要将工作目录设置到Python-3.1.1PCbuild中,以获取到动态库,至于静态库的包含,Include目录的指定,那自然也是少不了的。文件中需要包含Python.h文件,这也是必须的。

接口中

Py_Initialize()

Py_Finalize()

其他的根据需求,再引入相应的python builder 即可

1. Cython是什么?

它是一个用来快速生成Python扩展模块(extention module)的工具

语法是Python和c的混血

Cython作为一个Python的编译器,在科学计算方面很流行,用于提高Python的速度,通过OpenMPI库还可以进行吧并行计算。

2. Cython安装(Windows)

我的环境是win7 x64, python27, vs2010

安装的基础是有一个c编译器(这里以vs2010为例)

从http://cython.org下载安装包,解压到一目录,进入该目录,在cmd命令行中执行

python setup.py install

注:执行过程可能遇到问题:Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

解决方案:下载Microsoft Visual C++ Compiler for Python 2.7,点击直接安装即可。

3. 例子

例3.1:入门

创建hello.pyx,内容如下

def say_hello():

print "Hello World!"

创建setup.py,内容如下

from distutils.core import setup

from Cython.Build import cythonize

setup(name = 'Hello world app',

ext_modules = cythonize("hello.pyx"))

编译Cython代码

step1: 把.pyx文件被Cython便以为.c文件

step2: 把.c文件编译为可导入的使用模块.so(Windows下为.pyd)文件

1

2

python setup.py build

python setup.py install

注:可能出现问题:Unable to find vcvarsall.bat

原因:Python 2.7 会搜索 Visual Studio 2008.如果你电脑上没有这个版本的话就会报错。

如果装的是vs2010,那么在cmd命令行中执行

1

SET VS90COMNTOOLS=%VS100COMNTOOLS%

如果装的是vs2010,那么在cmd命令行中执行 

1

SET VS90COMNTOOLS=%VS110COMNTOOLS%

执行

1

2

3

>>>import hello

>>>hello.say_hello()

Hello World!

例3.2 通过静态类型提高速度

在Cython中可以通过标记静态类型来提高速度,凡是标记为静态类型的部分都会将动态语言类型变为简单的c代码,从而提速。

但是如果滥用静态类型,会降低可读性,甚至因类型设置不当导致错误类型检查造成速度降低。

例3.2.1 静态类型变量

Python原生态代码

compute.pyx

def f(x):

return x ** 2 - x

def integrate_f(a, b, N):

s = 0

dx = (b - a) / N

for i in range(N):

x += f(a + i * dx)

return s * dx

setup.py

from distutils.core import setup

from Cython.Build import cythonize

setup(

name = 'Hello world app',

ext_modules = cythonize("compute.pyx"),

)

test.py

import compute

import time

starttime = time.clock()

compute.integrate_f(3.2, 6.9, 1000000)

endtime = time.clock()

print "read: %f s" %(endtime - starttime)

执行

1

2

3

python setup.py build

python setup.py install

python test.py

结果

1

read: 0.332332 s

使用静态变量替换后的代码

compute2.pyx

def f(double x):

return x ** 2 - x

def integrate_f(double a, double b, int N):

cdef int i

cdef double s, dx

s = 0

dx = (b - a) / N

for i in range(N):

s += f(a + i * dx)

return s * d

setup2.py

from distutils.core import setup

from Cython.Build import cythonize

setup(

name = 'Hello world app',

ext_modules = cythonize("compute2.pyx"),

)

test2.py

import compute2

import time

starttime = time.clock()

compute2.integrate_f(3.2, 6.9, 1000000)

endtime = time.clock()

print "read: %f s" %(endtime - starttime)

执行

1

2

3

python setup.py build

python setup.py install

python test.py

结果

1

read: 0.109200s

结论

该测试用例,使用静态类型速度是不使用静态类型的3倍。

例3.2.2 静态类型函数

把compute2.pyx中的函数变为

cdef double f(double x):

return x ** 2 - x

def integrate_f(double a, double b, int N):

cdef int i

cdef double s, dx

s = 0

dx = (b - a) / N

for i in range(N):

s += f(a + i * dx)

return s * dx

结果

1

read: 0.084859 s

结论:比例子3.2.1速度又快了

例3.3 调用C函数

cdef extern from "math.h":

double sin(double)

double cos(double)

cpdef double Sin(double x):

return sin(x)

cpdef double Cos(double x):

return cos(x)

cpdef: 对于Python可使用的函数使用(为了使得在以后的Python程序中调用Sin,Cos函数,用cpdef,而不用cdef)

cdef: 对于C可使用的函数使用

请注意,上面的代码声明了 math.h 里的函数,提供给 Cython 使用。C编译器在编译时将会看到 math.h 的声明,但 Cython 不会去分析 math.h 和单独的定义。