Here is an example using SWIG:
The Python code which calls C++ function "inflow":
import inflow # importing C++ inflow library
nframes = 25
print 'calling inflow function in loop ...'
for i in xrange(0,1001):
z = inflow.inflow(""" arguments""")
""" code does something with z """
The C++ function will be as usual:
#include <iostream>
#include <vector>
inflow(/* arguments from Python*/)
{
/* code does something */
}
Now to interface with Python, here are the steps:
1) IMPORTANT - Make sure the C++ code you are trying to bind in this step has a different name than the one
given in the command. Else it will overwrite with swig code.
Lets say example_wrap.cpp is the file you want to interface with Python and "example.i" is the SWIG interface file. SWIG will generate a new file with the name example.cpp.
2) swig -c++ -python -o example_wrap.cpp example.i
3) g++ -I /usr/include/python2.7 -fPIC -c example_wrap.cpp -o example_wrap.o
4) g++ -shared -o _example.so example_wrap.o
Idea is that the compiled module name should start with an underscore, followed by the name.
5) Open Python in term, and say
from example import *
and then start calling the functions.
6) Source : http://www.iram.fr/~roche/code/python/SWIG.html#purpose
The interface file for the example would look something like this:
%module example
%{
#include "example.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
// Include the header file with above prototypes
%include "example.h"