Calling C++ function from python script

Pankaj pankajgode at
Sun Jan 29 05:01:32 CET 2006

See, i tell u, nothing is difficult in this world.

I achieved the thing which i was asking for.

Thanks for the advice.

I used this paper:

for creating python modules from C code.

Which is what i needed. In order to interface both things, we need
convert atleast one thing to other so i choosed to convert C code to
python module.

The steps which i followed were:
These steps were to create a python module from C code. So a mandatory
condition for .c file is : it should not have main function, and any
variable in any function called from main function should be declared

1. Creating wrapper from .i :
swig -python TestCase.i (where TestCase.i is interface file containing
declarations of functions and variables)
2. Creating .o's :
 gcc -c TestCase.c TestCase_wrap.c (TestCase_wrap.c is file genereted
by swig and is a wrapper for creating a python module)
3. Shared Library:
 ld -shared TestCase.o TestCase_wrap.o

Module was inserted as:
2. python
( In this python file i did: "import TestCase" and then used it as


So, we don't need to write any code to do this.

More information about the Python-list mailing list