Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 336 Bytes

call-c-in-python.md

File metadata and controls

28 lines (22 loc) · 336 Bytes

创建一个 C 文件 add.c

int add(int x, int y)
{
    return x + y;
}

编译 Linux 下的动态链接库

gcc -c -fpic add.c
gcc -shared -o libadd.so add.o

使用

>>> import ctypes
>>> add_lib = ctypes.CDLL("./libadd.so")
>>> add = add_lib.add
>>> add
<_FuncPtr object at 0x7ff4f2908dc0>
>>> add(1,2)
3