当前位置 博文首页 > python3调用c语言代码的全过程记录

    python3调用c语言代码的全过程记录

    作者:crazy_baoli 时间:2021-06-06 18:17

    开发环境

     linux: python3.5.2 + ubuntu-gnome-16.04-desktop-amd64

     windows:cygwin + powershell + python3.6

    胶水语言python为什么要调用c代码?

     c与python对比python不擅长"大量运算"任务,python程序优势在于:编写简单,适合"IO密集型作业"(比如打开文件,下载图片,运行脚本)。python作为知名的"胶水语言",快速实现"计算密集型作业"的方法就是"把c的代码拿过来直接用"。

     Python中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改,也正是如此奠定了这种方法的简单性。

     一、linux

     1. 准备C语言程序,保存为add.c

    #include <stdio.h>
     
    int add_int(int, int);
    float add_float(float, float);
     
    int add_int(int num1, int num2)
    {
        return num1 + num2;
    }
     
    float add_float(float num1, float num2)
    {
        return num1 + num2;
    }

     2. 编译成so库

     Ubuntu下执行:gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c

     3. 准备python代码,保存为python-c.py

    import ctypes
     
    #load the shared object file
    adder = ctypes.cdll.LoadLibrary('./adder.so')
     
    #Find sum of integers
    res_int = adder.add_int(4,5)
    print("4 + 5 = " + str(res_int))
     
    #Find sum of floats
    a = ctypes.c_float(5.5)
    b = ctypes.c_float(4.1)
     
    add_float = adder.add_float
    add_float.restype = ctypes.c_float
     
    print("5.5 + 4.1 = " + str(add_float(a, b)))

     4. 测试

     执行:python3 python-c.py

     结果如下:

     4 + 5 = 9

     5.5 + 4.1 = 9.600000381469727

     5. 说明

     在Python文件中,一开始先导入ctypes模块,然后使用cdll.LoadLibrary
     函数来加载我们创建的库文件。这样我们就可以通过变量adder来使用C类库中的函数了。当adder.add_int()被调用时,内部将发起一个对C函数add_int的调用。ctypes接口允许我们在调用C函数时使用原生Python中默认的字符串型和整型。

     而对于其他类似布尔型和浮点型这样的类型,必须要使用正确的ctype类型才可以。如向adder.add_float()函数传参时, 我们要先将Python中的十进制值转化为c_float类型,然后才能传送给C函数。这种方法虽然简单,清晰,但是却很受限。例如,并不能在C中对对象进行操作。

     二、windows

     1. 准备C语言程序,保存为add.c

     同上

     2. 编译成dll库

     cygwin下执行:gcc -shared -Wl,-soname,adder -o adder.dll -fPIC add.c

     3. 准备python代码,保存为python-c.py

    import ctypes
     
    #load the shared object file
    adder = ctypes.cdll.LoadLibrary('.\\adder.dll')
     
    #Find sum of integers
    res_int = adder.add_int(4,5)
    print("4 + 5 = " + str(res_int))
     
    #Find sum of floats
    a = ctypes.c_float(5.5)
    b = ctypes.c_float(4.1)
     
    add_float = adder.add_float
    add_float.restype = ctypes.c_float
     
    print("5.5 + 4.1 = " + str(add_float(a, b)))

     4. 测试

     powershell下执行: .\python-c.py

     会出现以下错误:

     OSError: [WinError 126]

     原因:adder.dll本身依赖其它库,需要将这些库一起复制到当前目录。

     cygwin下执行:ldd adder.dll

     提示依赖以下库

     将这些库copy到当前目录,执行:cp xxx.dll .

     powershell下再次执行:.\python-c.py

     结果如下:

    总结

    js