6

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!

1
  • I have the same problem, and I would love to hear if the answer helped you or if you found some other solution. Commented Jan 23, 2014 at 20:59

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.