Linking a SWIG wrapper with other libraries
Question: I have a C++ function that I want to call from Python. The function itself is
pretty simple, but it involves an IPC call that can only be done in C++. To
compile that C++ code requires linking a ton of other libraries in. I'm trying
to use SWIG for this. I have a Makefile that looks like this:

    
    
    all: swig object shared
    
    object: swig
      ${CC} -c ${MODULE}_wrap.cxx ${INCLUDES}
    
    shared:
      ${CC} -Wl,--verbose -shared ${MODULE}_wrap.o -o _${MODULE}.so
    
    swig:
      ${SWIG} -c++ -python ${MODULE}.i
    

With this, everything compiles fine, but then importing my module in Python
gives me "undefined symbol" errors. If I change the `shared` line to:

    
    
    shared:
      ${CC} -Wl,--verbose -shared ${MODULE}_wrap.o -o _${MODULE}.so ${LIBS}
    

it fails to compile with `collect2: ld returned 1 exit status` but doesn't
tell me exactly what the error is. The verbose linker output has a ton of
lines that say "attempt to open [some lib] failed" but a handful of those
lines say "succeeded". Could it just be the ones that say failed are
preventing linking from happening? Is what I'm trying to do even possible with
SWIG modules?


Answer: Yes, it is possible. You need to make sure the shared libraries your wrapped
C/C++ module depends on are resolved when Python loads the module. Check the
library (Swig generated) with `ldd` to see what libs it depends on. It is best
to set up rpath (runtime path; something like: `-rpath=/path/to/your/libs` for
gcc) to point to those libs' location(s) when you link your module.
Alternatively you can set `LD_LIBRARY_PATH` appropriately before running
Python.

