0

I have few shared libraries that I compiled and have dependencies as follows liba.so depends on nothing, libb.so depends on liba.so, libc.so depends on liba.so, and libd.so depends on liba.so, libb.so, libc.so

I ran ldd on libraries b-d and got this:

libb.so:

...some system/installed libs...
liba.so => ./ext/lib/liba.so
...some more system/installed libs...

libc.so:

...some system/installed libs...
liba.so => ./ext/lib/liba.so
...some more system/installed libs...

libd.so:

...some system/installed libs...
libc.so => ./ext/lib/libc.so
libb.so => ./ext/lib/libb.so
liba.so => ./ext/lib/liba.so
...some more system/installed libs...

now when I try to import libd in python I get an error like

OSError: ./lib/libd.so: undefined symbol: function_from_libb

I've read some other answers to this same sort of question and they suggest things like using ctypes.RTLD_GLOBAL or make sure the LD_LIBRARY_PATH env var is set properly but neither of these things have made a difference. when I try importing liba, libb, libc using CDLL it works just fine, so what is different with libd that would be causing this?

jdodle
  • 141
  • 1
  • 3
  • 11
  • How do you import it? Check with *strace* which libraries it loads. Also: https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value/58611011. – CristiFati Sep 02 '20 at 14:16

1 Answers1

0

I ended up solving this on my own so here is what I found out:

libb.so is a C compiled library, so when libd.so was compiled as C++ it tried to expose one of the functions it used from libb.so however libb.so was not compiled with extern "C" (I didn't think to do this since I was compiling it as C) so C++ tried exposing the name-mangled symbol for the C function and when CDLL loaded libd.so it couldn't find the symbol since it didn't exist as a c++ function.

The solution was to use this in the header files of my c code

#ifdef __cplusplus
extern "C" {
#endif
...my c code definitions...
#ifdef __cplusplus
}
#endif
jdodle
  • 141
  • 1
  • 3
  • 11