2

python包合集-cffi - Se7eN_HOU

 1 year ago
source link: https://www.cnblogs.com/Se7eN-HOU/p/16581001.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、cffi

  cffi是连接Python与c的桥梁,可实现在Python中调用c文件。cffi为c语言的外部接口,在Python中使用该接口可以实现在Python中使用外部c文件的数据结构及函数。

二、直接在python中通过cffi定义c函数并使用

  1、先通过pip3安装cffi :  pip3 install cffi

  2、编写测试代码:直接在 python 文件中 编写并执行 C语言代码

# test1.py 文件中

# 从cffi模块导入FFI from cffi import FFI # 创建FFI对象 ffi = FFI() # 使用cdef创建C语言的函数声明,类似于头文件 ffi.cdef("int add(int a, int b);") ffi.cdef("int sub(int a, int b);") #verify是在线api模式的基本方法它里面直接写C代码即可 lib = ffi.verify(""" int add(int a, int b) { return a+b; } int sub(int a,int b) { return a-b; } """) print(lib.add(1, 2)) print(lib.sub(1, 2))

  3、执行结果

root@ubuntu:~/test_cffi# python3 test1.py 
3
-1

三、加载已有C语言代码并执行

  1、创建 test2.c 文件,并写如下代码,注意这是一个 .c 的文件

#include <stdio.h>

// 函数声明
int add(int a, int b);
// 函数定义
int add(int a, int b)
{
    return a+b;
}
int mul(int a,int b);
int mul(int a,int b)
{
    return a*b;
}

  2、创建 test3.py 文件,并在 test3.py 中调用 test2.c 文件

from cffi import FFI
ffi = FFI()

# 就算在C语言的文件中定义了,这里在时候前还是需要声明一下
ffi.cdef("""
    int add(int a, int b);
    int mul(int a,int b);
""")

#verify是在线api模式的基本方法它里面直接写C代码即可
lib = ffi.verify(sources=['test2.c'])
print(lib.add(1,2))
print(lib.mul(1,2))

  3、运行结果

root@ubuntu:~/test_cffi# python3 test3.py 
3
2

四、打包C语言文件为扩展模块提供给其他 python 程序使用

  1、创建 test4.py 文件,其内容如下

import cffi

ffi = cffi.FFI() #生成cffi实例

ffi.cdef("""int add(int a, int b);""") #函数声明
ffi.cdef("""int sub(int a, int b);""")


# 参数1:为这个C语言的实现模块起个名字,类似于,这一块C语言代码好像写在一个文件中,而这就是这个文件的名字,既扩展模块名
# 参数2:为具体的函数实现部分
ffi.set_source('test4_cffi', """
    int add(int a, int b)
    {
        return a + b;
    }
    int sub(int a, int b)
    {
        return a - b;
    }
""")

if __name__ == '__main__':
    ffi.compile(verbose=True)

  2、 执行: python3 test4.py 执行过程如下

root@ubuntu:~/test_cffi# python3 test4.py 
generating ./test4_cffi.c
the current directory is '/root/test_cffi'
running build_ext
building 'test4_cffi' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c test4_cffi.c -o ./test4_cffi.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 ./test4_cffi.o -o ./test4_cffi.cpython-35m-x86_64-linux-gnu.so
root@ubuntu:~/test_cffi# 

  3、执行后多三个文件,分别是 .c, .o , .so 结尾的文件

  • test4_cffi.c
  • test4_cffi.cpython-35m-x86_64-linux-gnu.so
  • test4_cffi.o

  4、编写 test5.py, 在 test5.py 中使用test4_cffi 扩展模块,如下

from test4_cffi import ffi, lib

print(lib.add(20, 3))
print(lib.sub(10, 3))

  5、运行结果如下

root@ubuntu:~/test_cffi# python3 test5.py 
23
7

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK