【发布时间】:2014-07-23 09:36:35
【问题描述】:
我正在尝试使用 ctypes 从 Python 调用 c 函数。
我有三个 c 文件,两个带有用于 USB 设备的驱动程序(usb-1024LS.c 和 pmd.c),一个带有一些功能(myTest_1024LS.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 中的一个函数。
我的 Python 脚本:
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()
findInterface 调用 pmd.c 中的函数,而 pmd.c 中的函数调用 libusb 中的函数。
我是从 Python 调用 c 函数的新手,所以我需要我能得到的所有帮助! 谢谢!
【问题讨论】:
-
如果从运行 python/程序的终端运行“ldd /path-to-folder/usb1024LS_with_py/pmd.so”,输出是什么?是否所有依赖项都已解决/可用?
-
然后我得到这个输出: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)