Embedding C into Python

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Oct 25 07:06:22 EDT 1999


Jeff Shipman - UC <jeff at nmt.edu> writes:

> /tmp/ccype0NL.o: In function `blah_add':
> /tmp/ccype0NL.o(.text+0x24): undefined reference to `PyArg_ParseTuple'
> /tmp/ccype0NL.o(.text+0x4f): undefined reference to `Py_BuildValue'
> /tmp/ccype0NL.o: In function `initblah':
> /tmp/ccype0NL.o(.text+0x7b): undefined reference to `Py_InitModule4'
> collect2: ld returned 1 exit status
> 
> I'm including Python.h so I don't know why this is happening. Also, when
> this compiles, will this allow me to say 'import blahmodule' like
> I want? Or is there more that has to be done in order to put a C module
> into Python?

If your platform (Linux?) supports shared libraries, then yes: If you
create a shared library from your module, you can import it with
'import bla' (module.so is appended by Python automatically). To
compile it, you must write

  gcc -shared -fPIC -o blahmodule.so blahmodule.c

though: -shared tells gcc you want a shared library, -fPIC tells it
you want 'position-independent code' (mandatory for shared libraries),
and you want the executable not be named 'a.out'.

The symbols you are missing are defined in the Python interpreter
proper (/usr/local/bin/python); when the module is loaded, those
symbols will be there.

Alternatively, you could create a new Python interpreter that has you
module compiled-in; to do so, you need to write a config.c, and link
with libpython1.5.a.

Hope this helps,
Martin





More information about the Python-list mailing list