pythonlinkerctypesgslscalapack

python ctypes link multiple shared library with example gsl gslcblas


I want to use some functions from a shared library in python. From the python doc, I know ctypes is a good choice. However such library have some undefined symbols and I should link it to another shared library to get the symbols.

In g++, it is simple: g++ main.cpp -la -lb. The function I need is in liba.so, and liba.so has some undefined function which can be solved in libb.so.

But how to do that in ctypes? ctypes.cdll.LoadLibrary('liba.so') said that there are some undefined symbols, how to tell ctypes to find libb.so? Because ldd liba.so not show a link to libb.so.

Example: I want to use some functions in gsl. In g++:

g++ main.cpp -lgsl -lgslcblas

and ldd libgsl.so does not show a link to libgslcblas

In python:

ctypes.cdll.LoadLibrary('libgsl.so')

how to tell ctypes to find libgslcblas?

The same problem also happen if I use scalapack. I use ubuntu 16.04


Solution

  • This old answer tells to apply mode=ctypes.RTLD_GLOBAL, i.e. in this case

    import ctypes
    
    dll1 = ctypes.CDLL('libgslcblas.so', mode=ctypes.RTLD_GLOBAL)
    dll2 = ctypes.CDLL('libgsl.so')