潇洒郎: Python调用C/C++ dll文件 传入参数
1.c
int fib(int n) // 斐波那契数列
{
if (n<2)
{
return 1;
}
else
{
return fib(n-1)+ fib(n-2);
}
}
2、 生成dll 文件
gcc -m64 --share 1.c -o 1.dll
3、test.py
import ctypes
# GIL 在调用期间释放
dll = ctypes.cdll.LoadLibrary('1.dll')
fib= dll.fib
fib.argtypes=[ctypes.c_int] # 传入整型参数
fib.restype=ctypes.c_int # 返回整型参数
a=fib(ctypes.c_int(8)) # 函数fib传入整型参数
print(a)
斐波那契数列
1 : 1
2 : 2
3 : 3
4 : 5
5 : 8
6 : 13
7 : 21
8 : 34
9 : 55
ctypes数据类型与c数据类型,python数据类型对应
启发连接:https://www.cnblogs.com/TQCAI/p/8881530.html