I was trying to load a shared library compiled from C source into Python with ctypes. The shared library (named "libsub.so" below) used libusb libraries. This is what "make" did:
gcc -c -O2 -Wall -Werror -g -I../src -I../boot/vnd/fw -I. -fPIC -DLIBUSB_1_0 -I/usr/include/libusb-1.0 -o libsub.o libsub.c
gcc -shared -Wl,-soname,libsub.so -o libsub.so libsub.o
And I tried Python after that:
import ctypes
h = ctypes.cdll.LoadLibrary('./libsub.so')
However, I got an error like this
OSError: ./libsub.so: undefined symbol: libusb_open
I found "libusb_open" was actually a function of libusb header in "/usr/include/libusb-1.0/libusb.h", which was already included in the source of this library "libsub.c".
A few posts in StackExchange talked about such "undefined symbol" errors when loading C++ shared libraries with ctypes, and problems were solved by changing compiler from gcc to g++. However, the source I had was written in C --- so it might be a different situation (actually I tried g++ to compile this source but got a bunch of errors). Can anyone point out what I am missing here? Thanks!
I believe you should require, in the second line, that your libsub.so be linked together with a pointer to the original libusb.so:
gcc -shared -Wl,-soname,libsub.so -lusb -o libsub.so libsub.o
^^^^^
Maybe you also need to specify the path to libusb.so with -L/path
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With