Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ctypes loading error: undefined symbol

Tags:

python

ctypes

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!

like image 928
shva Avatar asked Jul 18 '13 06:07

shva


1 Answers

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.

like image 133
Armin Rigo Avatar answered Sep 21 '22 06:09

Armin Rigo