2014-07-23 2 views
0

ctypes를 사용하여 파이썬에서 c 함수를 호출하려고합니다.Python에서 C 함수 호출

USB 장치 용 드라이버 (usb-1024LS.c 및 pmd.c)와 드라이버를 테스트하는 몇 가지 기능 (myTest_1024LS.c)이있는 두 개의 c 파일이 있습니다. 드라이버 c- 파일은 libusb를 사용하고 있습니다. 스크립트를 실행하려고하면 컴파일러가 libusb를 찾지 못하는 것처럼 보입니다. 오류 :

File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__ 
self._handle = _dlopen(self._name, mode) 
OSError: /path-to-folder/usb1024LS_with_py/pmd.so: undefined symbol: usb_get_driver_np 

usb_get_driver_np는 libusb의 함수입니다.

내 파이썬 스크립트 : findInterface는 pmd.c에서 기능과 pmd.c에서 함수를 호출

import ctypes 

pmd = ctypes.CDLL('/home/oysmith/NetBeansProjects/MCCDAQ/usb1024LS_with_py/pmd.so'); 
usb1024LS = ctypes.CDLL('/home/oysmith/NetBeansProjects/MCCDAQ/usb1024LS_with_py/usb-1024LS.so'); 
testFunc = ctypes.CDLL('/home/oysmith/NetBeansProjects/MCCDAQ/usb1024LS_with_py/myTest_1024LS.so'); 

# declare the function we use 
testFunc.writeByte.argtypes = (ctypes.c_void_p,) 
testFunc.writeByte.restype = ctypes.c_void_p 

testFunc.findInterface.argtypes = (ctypes.c_void_p,) 
testFunc.findInterface.restype = ctypes.c_void_p 

pmd.PMD_GetInputReport.argtypes = (ctypes.c_char_p, ctypes.c_uint, ctypes.c_uint, ctypes.c_int, ctypes.c_int,) 
pmd.PMD_GetInputReport.restype = ctypes.c_char_p, ctypes.c_uint, ctypes.c_uint, ctypes.c_int, ctypes.c_int 

#call C function 
testFunc.findInterface() 

libusb를의 함수를 호출합니다.

저는 파이썬에서 c 함수를 호출하는 것이 처음이므로, 제가 얻을 수있는 모든 도움이 필요합니다! 감사합니다.

+1

python/프로그램을 실행중인 터미널에서 "ldd /path-to-folder/usb1024LS_with_py/pmd.so"를 실행하면 출력은 무엇입니까? 모든 의존성이 해결 되었습니까? –

+0

다음과 같이 출력됩니다 : linux-vdso.so.1 => (0x00007fffcfe8b000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0693615000) /lib64/ld- linux-x86-64.so.2 (0x00007f0693bf7000) –

답변

0

나는 최근에 이와 비슷한 질문을했지만, 문제가 내 문제와 관련이 있는지 (나는 초급자인지), 호출 규칙 문제가 있었는지, 그리고 정의되지 않은 기호가 있다는 것을 알았 기 때문에 내 문제 : Wrapping c++ functions with ctypes 및 그 대답을 읽을 것을 권유합니다. 읽는 데 그리 길지 않습니다.

당신은 컴파일러일지도 모른다고했지만 파이썬 스크립트를 실행하고 있다고 할 수 있습니다. 스크립트를 실행하기 전에 파이썬 인터프리터 또는 C 컴파일러를 의미합니까? 파이썬없이 libusb와의 통신을 테스트하기 위해 C 라이브러리로 C 메인 프로그램을 빌드 해본 적이 있습니까?

이 정보가 도움이되기를 바랍니다.

+0

나는 그것을 살펴볼 것입니다. 고마워요! 나는 C 메인 프로그램을 만들었고 함수가 작동하는지 확인했다. 그래, 난 파이썬 인터프리터, C 파일의 컴파일 잘 작동합니다! –