2017-04-10 17 views
2

변환하는 방법을 알고하지 마십시오ctypes.ArgumentError 다음과 같이 내가 C 라이브러리에서 함수를 정의하는 매개 변수

int* Test(char *str1,int id1,char *str2,float val,float *ls) 

내가 파이썬에서 사용하려는 그래서 다음 파이썬 코드 쓰기 :

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 

내 코드에 문제가 있습니까? 나를 도와 주셔서 감사합니다.

+0

(http://www.swig.org/tutorial.html)? –

+3

시도 :'lib.Test.argtypes = [c_char_p, c_int, c_char_p, c_float, POINTER (c_float)]'(모두'ctypes'에 의해 정의 됨). – CristiFati

+0

또한'ndpointer'는 _numpy _와 관련이있는 것으로 보입니다. _ 그렇다면 _ctypes_와 함께 사용하면 효과가 없습니다. – CristiFati

답변

1

@CristiFati가 제공하는 솔루션이 내 문제를 해결합니다. 나는 그의 대답을 코멘트에 복사하고 여기에 선물한다. 고마워 .CristiFati.

보십시오 : 당신이 꿀꺽 꿀꺽를 사용하지 않으

lib.Test.argtypes = [c_char_p, c_int, c_char_p, c_float, POINTER(c_float)] (all defined by ctypes)