我有以下 C 代码。我正在尝试使用 ctypes
从 Python 调用此函数:
int add ( int arr [])
{
printf("number %d \n",arr[0]);
arr[0]=1;
return arr[0];
}
我使用以下方法编译了它:
gcc -fpic -c test.c
gcc -shared -o test.so test.o
然后将其放入/usr/local/lib
。
对此的 Python 调用是:
from ctypes import *
lib = 'test.so'
dll = cdll.LoadLibrary(lib)
IntArray5 = c_int * 5
ia = IntArray5(5, 1, 7, 33, 99)
res = dll.add(ia)
print res
但我总是得到一些大数字,例如 -1365200
。
我也试过:
dll.add.argtypes=POINTER(c_type_int)
但它不起作用。
最佳答案
相反,尝试:
dll = cdll.LoadLibrary('test.so')
res = dll.add(pointer(c_int(5)))
print res
关于python - 使用ctypes从python调用C函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25407258/