There is no doubt that Numpy, executes a huge amount of tasks ? In fact, libraries like Numpy are not written entirely in Python, some parts of the library are written in C, which provides better performance. After writing the C code, we wrap them in Python code, which acts as an interface for those C codes. We can then call C functions using Python syntax, where the actual processing is done in C behind the scenes, and the result is returned back as a Python object. In this article, we will see how to create a Python wrapper for our C program on Linux systems using SWIG software.
What is SWIG strong >
In a nutshell, SWIG — it is a compiler that accepts C / C ++ declarations and wraps it up to access those declarations from other languages like Python, Tcl, Ruby, etc.
Usually this does not require any changes to existing code and creates an interface within a minute.
Reasons for creating a wrapper
In many cases we need wrappers, the following are — p>
To download SWIG directly from the apt repository, enter the following commands — p>
sudo apt-get update sudo apt-get install swig
Writing a Wrapper using SWIG
Let`s look at this piece of code at C with two functions and one global variable —
Here is our header file —
First we have to create the SWIG interface file strong>. This file contains prototypes of ANSI C functions and variable declarations. Here —
Now we will create the wrapper code using a command like p>
$ swig -python gfg.i
This command generates wrapper code named “gfg_wrap.c” . These files contain a bloated version of our C source with different error handling code, etc. Generated another file “gfg.py”, which is the module we are importing into our python script.
After that we have to generate position-independent code to be used in the shared library by compiling “gfg_wrap.c” and “gfg.c” with the following command:
$ gcc -c -fpic gfg_wrap.c gfg.c -I / use / include / python2.7
Replace python2.7 with your Python version. This will create two object files
“Gfg_wrap.o” and “gfg.o” . In the above command —
Note: if you receive an error message such as the following possible causes —
To get & # 39; Python.h & # 39; you have to install Python-dev, using the following command — p >
$ sudo apt-get install python-dev
To find the correct path to Python.h, run the following command —
$ python-config --cflags
This will output something like this —
Now replace the path in the compile command with this one for python2.7 or change the version to python3.5 for Python 3.5.
Now, finally, we have to link the generated object files together to create a shared object, which is similar to the DLL files on Windows. Use the following command, this will generate a shared object file “_gfg.so” —
$ gcc -shared gfg.o gfg_wrap.o -o _gfg.so pre >
We are now ready to test the Python shell by importing it. Make sure you are in the directory with this shell file.& gt; & gt; & gt; import gfg & gt; & gt; & gt; res = fact (5) & gt; & gt; & gt; res 120 & gt; & gt; & gt; res = my_mod (5,2) & gt; & gt; & gt; res 1 & gt; & gt; & gt; gfg.cvar.myvar 3.4
Here C variables are available as.
Compiling and linking using distutils
Instead of typing commands and figuring out what compilation options are needed to compile files, we can automate this with distutils. Create as shown below —
Now write the following commands to compile and install the module —
$ python setup.py build_ext --inplace
This should look like do something like this on the terminal —