Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG and C++ shared library

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.

like image 619
Veles Avatar asked Apr 12 '12 15:04

Veles


People also ask

What is SWIG library?

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.

What is SWIG C++?

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.

What is a shared library C++?

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).

How does SWIG work Python?

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.


1 Answers

I've put together a complete example for you:

Header file:

(mylib.h)

class Foo {
};

void bar(const Foo&);

Implementation:

#include "mylib.h"
#include <iostream>

void bar(const Foo& f) {
  std::cout << &f << std::endl;
}

Compile the library:

g++ -fPIC -Wall -Wextra -shared mylib.cc -o libmylib.so

SWIG interface to wrap the library:

%module mylib

// Make mylib_wrap.cxx include this header:
%{
#include "mylib.h"
%}

// Make SWIG look into this header:
%include "mylib.h"

Compile Python module:

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

Run

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.

like image 155
Flexo Avatar answered Oct 19 '22 05:10

Flexo