I have a binary which can load .so
shared objects to extend functionality. These extensions are coded in C++ but I want to use some pre-coded python functions so I make use of Python C API. So far so good.
Calls to Python functions work nicely, but if, in Python, I import spidev module I get the following error:
import spidev
ImportError: /usr/local/lib/python2.7/dist-packages/spidev.so: undefined symbol: _Py_ZeroStruct
Segmentation fault
If I import standard python modules (sys, os, argparse...) there's no problem.
What could be the problem?
N.B.: I know that I could use spidev directly from C++ but I wanted to reuse existing python code as much as possible.
UPDATE:
As @BrianCain and @qarma pointed out, it can be a problem related with dependencies to libpython
so I include ldd
outputs:
$ ldd myextension.so
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f89000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6f5f000)
libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0xb6f54000)
libutil.so.1 => /lib/arm-linux-gnueabihf/libutil.so.1 (0xb6f49000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0xb6ed8000)
libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0xb6c47000)
libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6c1f000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6af0000)
/lib/ld-linux-armhf.so.3 (0xb6fa2000)
libz.so.1 => /lib/arm-linux-gnueabihf/libz.so.1 (0xb6ad2000)
$ ldd /usr/local/lib/python2.7/dist-packages/spidev.so
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6ed3000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6ea9000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6d7a000)
/lib/ld-linux-armhf.so.3 (0xb6eed000)
UPDATE2:
Output of spidev installation.
$ sudo pip install spidev
Downloading/unpacking spidev
Downloading spidev-2.0.tar.gz
Running setup.py egg_info for package spidev
Installing collected packages: spidev
Running setup.py install for spidev
building 'spidev' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/src/linux/include -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv6l-2.7/spidev_module.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv6l-2.7/spidev_module.o -o build/lib.linux-armv6l-2.7/spidev.so
Successfully installed spidev
Cleaning up...
$ ldd /usr/local/lib/python2.7/dist-packages/spidev.so
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f97000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6f6d000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6e3e000)
/lib/ld-linux-armhf.so.3 (0xb6fb1000)
Still not depending on libpython
...