ctypes.ArgumentError:不知道如何参数
问题描述:
转换我在C中定义库中的函数如下:ctypes.ArgumentError:不知道如何参数
int* Test(char *str1,int id1,char *str2,float val,float *ls)
我想在Python中使用它,所以我写了下面的Python代码:
str1 = 'a'
str2 = 'b'
id1 = 0
val = 1.0
system('g++ -c -fPIC libtraj.cpp -o test.o')
system('g++ -shared -Wl,-soname,test.so -o test.so test.o')
lib = cdll.LoadLibrary('./test.so')
num_item = 100000
ls = (ctypes.c_float * num_item)()
lib.Test.restype = ndpointer(dtype=ctypes.c_int, shape=(num_item,))
lib.Test.argtypes = [c_char_p]
a = create_string_buffer(str1)
b = create_string_buffer(str2)
ls_id = lib.Test(a,id1,b,val,ctypes.byref(ls))
然后我运行这个python程序。我遇到错误说:
ls_id = lib.Test(a,id1,b,val,ctypes.byref(ls))
ctypes.ArgumentError: argument 4: <type 'exceptions.TypeError'>: Don't know how to convert parameter 4
我的代码有什么问题? 谢谢大家的帮助!
答
@CristiFati提供的解决方案解决了我的问题。我只是在评论中复制他的答案,并在这里呈现。谢谢@CristiFati。
尝试:
lib.Test.argtypes = [c_char_p, c_int, c_char_p, c_float, POINTER(c_float)] (all defined by ctypes)
(http://www.swig.org/tutorial.html)? –
尝试:'lib.Test.argtypes = [c_char_p,c_int,c_char_p,c_float,POINTER(c_float)]'(全部由'ctypes'定义)。 – CristiFati
另外'ndpointer'似乎与_numpy _有关,如果是的话,那么将它与_ctypes_混合的机会将不起作用。 – CristiFati