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?

9 of 10 people (90%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

How can I import a library and source written in c into python?

May 17th, 2000 02:19
unknown unknown, Tamer Fahmy, Roger Hansen


There are actually 2 possibilities I know of:
1. write a wrapper Python module to your C library.
   The document on http://www.python.org/doc/current/ext/ext.html
   should provide you with information how to do this.

2. write a C program, that uses your library in all needed aspects
   and call it with os.popen() with the parameters provided you need.

I would consider number 2 a rude hack but maybe it is quicker to do. ;)

This is a good solution if you only have to wrap a few functions.
However if it's a library I would suggest SWIG 
<Url: http://swig.sourceforge.net/ >
since wrapping lots of functions are boring and error-prone. But you
should look at the "Extending ..." document above to get the feeling
of things.