python使用ctype调用C链接库

相对于 传统的C调用,使用ctype实在是太简单了

编写一个动态链接库ctype_test.c,
#include <stdlib.h>

int foo(int a, int b)
{
    printf("Your input %i and %i\n", a, b);
    return a + b;
}

编译
gcc -o ctype.so -shared -fPIC ctype_test.c


在python下试用一下吧
import ctypes
ll = ctypes.cdll.LoadLibrary # 我这是在linux下,windows调用windll之类的
lib = ll("./ctype.so")
lib.foo(1, 3)

你可能感兴趣的:(python调用c,ctype)