I have a C++ library (let’s call it mylib) which compiles to libmylib.so file in /usr/local/lib and I have a bunch of header files in a directory called my lib in /usr/local/include.
Now the thing I wanted to do (for starters) is just use one of the header files (it contains information about a class my library is offering) with SWIG to generate the mylib_wrap.cxx file and then compile it and link it against the existing mylib.so. So that I can instance my class in Python.
Is this the right approach/idea? How would the compile and linking command look like (not exactly of course)? I am trying to generate a Python binding.
I’ve put together a complete example for you:
Header file:
(mylib.h)
Implementation:
Compile the library:
SWIG interface to wrap the library:
Compile Python module:
Note that we linked the Python module against the library. If it wasn’t in the current directory you’d need to specify the library path. SWIG expects the native part of Python module to be called _module.so
Run
Here I made sure the shared objects we just built are on the library path by setting LD_LIBRARY_PATH appropriately.