Interfacing C and Python using SWIG

In this test I use 4 files to show how this process works.

  1. module.c
  2. swigtest.py
  3. Makefile
  4. myso.i

Makefiles need to have a TAB on the line after the colon and that is odd, but that is just the way it is. The file that does all the work is Makefile which contains this:

all: swig -python myso.i gcc -fpic -c module.c myso_wrap.c -I/usr/include/python2.6/ gcc -shared module.o myso_wrap.o -o _myso.so

The control file for SWIG is myso.i and contains this:

%module myso extern void indicate_link(const char* passedString);

The C code is in module.c and contains this:

#include <stdio.h> void indicate_link(const char* passedString) { printf("STD C interface - passed string = %s.\n", passedString); }

The python file to test this is swigtest.py and contains this:

#!/usr/bin/env python import _myso _myso.indicate_link(' Successful Swig and C and Python test')

The whole process is then done by typing make, and then ./swigtest.py

The methods of interfacing languages varies and this is just one way.

Many other things need to be known and chmod +x swigtest.py is one. Also you may have to sudo apt-get install swig.

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen