3

Possible Duplicate:
Integrate Python And C++

I am the python GUY and do web stuff in django. I want to know that how easy or hard it is to integrate python and C++ using SWIG.

Is it efficient/easy to do or should leave C++ and code the things in python.

There are some things C++ files which have been coded already by the past programmers. I know that SWIG is used to integrate languages but i don't know what practical implications or problems are there for using it.

or There is no use of it and re-writing code in Python is the better choice.

Community
  • 1
  • 1
Mirage
  • 30,868
  • 62
  • 166
  • 261

3 Answers3

6

Using swig is easy. Usually easier than people think. See http://www.swig.org/tutorial.html.

Basically you just a have to write an interface file *.i that just includes the headers you need.
example.i :

 %module example
  %{
  /* Includes the header in the wrapper code */
  #include "header.h"
  %}

  /* Parse the header file to generate wrappers */
  %include "header.h"

Then use SWIG to generate a wrapper. Compile it with your c++ code and you're done:

$ swig -python example.i
$ g++ -c example.cc example_wrap.cc \
    -I/usr/local/include/python2.1
$ ld -shared example.o example_wrap.o -o _example.so

You got your python module.

>>> import example
>>> example.foo()
log0
  • 10,489
  • 4
  • 28
  • 62
4

I often use swig for work in order to translate code to multiple languages and it is a really useful tool.

In order to understand what is the best way for you, you should observe your software interfaces. If you c++ classes have methods that only receive and returns simple types like strings or stl structures, you can be confident that your code doesn't have problems being "swigged". The problems come to you when you have complex interfaces.

An another issue with python is the presence of GIL that in multithreaded environments (with blocking calls in c++ side) can cause a lot of problems and involves in a complex management (using python-dev library in your c++ code).

A suggestion if your destination code is written only in python is to take a look of boost-python libraries that, being more specialized, are often more simple to use with python. The problems with multithreaded environments, however, remain.

Sebastiano Merlino
  • 1,273
  • 12
  • 23
  • I need to integrate the C++ classes with my django , will SWIG work there as well – Mirage Jul 09 '11 at 19:52
  • django is a python library. Your problems (if there are problems) come from C++ code. If you use this C++ code inside django, surely you don't have blocking code. It should work I think, the complexity depends on your C++ interfaces. – Sebastiano Merlino Jul 09 '11 at 20:04
3

How to use SWIG to interface python and C++. (Tongue in cheek)

  • Generate a .i file that represents some C++ header. Even though SWIG parses (tries to parse) your header file, it still needs a swig interface file to tell swig what is to be imported.
  • Solution: Simple! Tell one of your project members to write a C++ header file parser that generates a .i file.
  • Next run swig against that .i file.
  • Then watch it fail.
  • Edit the header file and insert #ifndef SWIG ... #endif around the code that SWIG doesn't understand.
  • Rinse and repeat.
  • Once it works, watch your 50 line c++ header file balloon into a 5000+ line swig .cpp file, and a somewhat smaller .py file.
  • Do the same with all of the header files that you want to be wrapped.
  • Compile your new and improved C++ application with hooks callable by python.
  • Go to lunch. The compile and link won't be done anytime soon.
David Hammen
  • 32,454
  • 9
  • 60
  • 108