faqts : Computers : Programming : Languages : Python : Common Problems : C/C++ Code

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 5 of 6 people (83%) answered Yes

Entry

How does python invoke external C function?

Jul 16th, 2000 21:40
unknown unknown, Wolfgang Grafen


I would recommend SWIG for a first step. You don't have to learn a lot
about extension modules with SWIG. Often it is easy like this (for sun 
solaris 2.5):
swig -python -module a a.c
gcc -O02 -DSWIG -c -fpic a.c a_wrap.c \
-I/user/freepool/sparc-sun-solaris2.5.1/lib/python/python1.52/include/py
thon1.5/
ld -G a.o a_wrap.o -o a.so
Then in Python you can use it like it is.
Python 1.5.2 (#33, May 17 2000, 17:03:52)  [GCC 2.7.2] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import a
>>> dir(a)
['__doc__', '__file__', '__name__', 'hex2bin', 'hex2bin_char',
'hex2bin_expand']
>>>
SWIG has a very good documentation. You can build extensions for PERL
and TCL the same way
http://www.swig.org
There is also another Corba-like Package called ILU. Probably SWIG is
more easy to use while ILU might be more powerful.
ftp://ftp.parc.xerox.com/pub/ilu/ilu.html