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.
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.
A shared library is an object module that can be loaded at run time at an arbitrary memory address, and it can be linked to by a program in memory. Shared libraries often are called as shared objects. On most UNIX systems they are denoted with a . so suffix and Microsoft refer to them as DLLs (dynamic link libraries).
In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates a wrapper needed to access those declarations from other languages like Python, Tcl, Ruby etc. It normally required no changes in existing code and create an interface within a minute. Building interpreted interface for existing C programs.
I've put together a complete example for you:
(mylib.h)
class Foo {
};
void bar(const Foo&);
#include "mylib.h"
#include <iostream>
void bar(const Foo& f) {
std::cout << &f << std::endl;
}
g++ -fPIC -Wall -Wextra -shared mylib.cc -o libmylib.so
%module mylib
// Make mylib_wrap.cxx include this header:
%{
#include "mylib.h"
%}
// Make SWIG look into this header:
%include "mylib.h"
swig -Wall -c++ -python mylib.i g++ -fPIC -Wall -Wextra -shared mylib_wrap.cxx -o _mylib.so -L. -lmylib -I/usr/include/python2.7/ -lpython2.7
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
LD_LIBRARY_PATH=. python Python 2.7.2+ (default, Nov 30 2011, 19:22:03) [GCC 4.6.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mylib >>> i=mylib.Foo() >>> mylib.bar(i) 0x28cc100 >>> mylib.bar(i) 0x28cc100 >>> mylib.bar(mylib.Foo()) 0x28b3b10
Here I made sure the shared objects we just built are on the library path by setting LD_LIBRARY_PATH appropriately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With