[Tutor] C and python together

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 6 Jul 2001 18:02:03 -0700 (PDT)


On Sat, 7 Jul 2001, Matthew Vogel wrote:

> I have written a C library of functions and now i want to use python to
> call those functions directly from python. I have been informed that i
> will need some special header files and such in order for this to work.
> I was just wondering if anyone could give me some information on what
> header files i might need and how to actually call a C function from my
> library, in python.
> Note: I DONT want to create a main C program that prints to standard out
> the results from each of the library functions when given certain
> command line args, through python.
> I actually need to call the C functions from inside python.

It sounds like you'll want the extension module capabilites in Python.  
Are you running on Windows or a Unix?  In either case, you'll need to
download the source to Python, because it comes with tools to help build
extensions.  Python's source code can be found here:

    http://python.org/ftp/python/2.1/

as Python-2.1.tgz.


Making an extension module is an advanced topic, so it's not as fully
documented as the rest of the Python system.  You can take a look at:

    http://python.org/doc/current/ext/ext.html

which gives a tutorial on the art of making extensions.  The example on
their tutorial shows how to run C's system() function from Python.  The
dry details about the Python/C API lies here:

    http://python.org/doc/current/api/api.html

Core Python Programming, by Wesley Chun, also gives examples on writing
Python extensions, so you may want to look into that book as well.


By the way, you can ask questions about making extension modules here, but
you might also want to ask on a forum like comp.lang.python just in case
we can't answer your question well.


Good luck to you!