【发布时间】:2014-08-20 14:23:35
【问题描述】:
我有以下 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)
但它不起作用。
【问题讨论】:
标签: python c call wrapper ctypes