我有如下的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)
但它无法正常工作。