Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can i integrate c++ and Python with SWIG [duplicate]

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.

like image 782
Mirage Avatar asked Feb 21 '26 10:02

Mirage


1 Answers

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()
like image 187
log0 Avatar answered Feb 23 '26 23:02

log0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!