0

I am trying to call c-functions from Python using ctypes.

I have three c files, two with drivers(usb-1024LS.c and pmd.c) for a USB device and one with some functions(myTest_1024LS.c) to test the drivers. The driver c-files are using libusb. When I try running my script it seems like the compiler doesn't find the libusb. I get the error:

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 is a function in libusb.

My Python script:

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()

Where findInterface call a function from pmd.c and the function in pmd.c call a function in libusb.

I am new to calling c functions from Python, so I need all the help I can get! Thanks!

  • 1
    If you run "ldd /path-to-folder/usb1024LS_with_py/pmd.so" from the terminal that you are running python/your program from, what is the output? Are all the dependencies resolved/available? – Tom Dalton Jul 23 '14 at 09:43
  • Then I get this output: 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) – Øystein A. Smith Jul 23 '14 at 11:11

1 Answers1

0

I asked a similar question recently, but I don't know if your problem is related to mine(I am a beginner), I had a calling convention problem, and you have undefined symbol, which looks close, so I advise you to read what was my problem : Wrapping c++ functions with ctypes and its accepted answer, it's not very long to read.

You said it could be the compiler, but you're running the python script, right ? Do you mean the python interpreter, or the C compiler before running the script ? Have you tried building a C main program with your C libraries to test the communication with libusb without any python ?

I hope this helps.

Community
  • 1
  • 1
PhilDenfer
  • 270
  • 4
  • 13
  • I will take a look a that, thanks! I have made a C main program and verified that the functions work. Yeah, I ment the python interpreter, compiling of the c files works fine! – Øystein A. Smith Jul 23 '14 at 10:18