zoukankan      html  css  js  c++  java
  • Python调用C语言

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

    示例如下

    实现两数求和的C代码,保存为add.c

    //sample C file to add 2 numbers - int and floats
    
    #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;
    
    }

    接下来将C文件编译为.so文件(windows下为DLL)。下面操作会生成adder.so文件

    #For Linux
    $  gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c
    
    #For Mac
    $ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c
    
    #For windows
    $
    gcc -shared -Wl,-soname,adder -o adder.dll -fPIC add.c
    
    

    现在在你的Python代码中来调用它

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

    输出如下

    Sum of 4 and 5 = 9
    Sum of 5.5 and 4.1 =  9.60000038147
    

    在这个例子中,C文件是自解释的,它包含两个函数,分别实现了整形求和和浮点型求和。

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

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

  • 相关阅读:
    [LeetCode] 493. Reverse Pairs 翻转对
    [LeetCode] Super Washing Machines 超级洗衣机
    [LeetCode] Perfect Number 完美数字
    [LeetCode] 483. Smallest Good Base 最小的好基数
    [LeetCode] Sliding Window Median 滑动窗口中位数
    [LeetCode] Diameter of Binary Tree 二叉树的直径
    [LeetCode] 01 Matrix 零一矩阵
    [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
    [LeetCode] Reverse String II 翻转字符串之二
    [LeetCode] Minimum Time Difference 最短时间差
  • 原文地址:https://www.cnblogs.com/lovephysics/p/7237227.html
Copyright © 2011-2022 走看看