[Pythonmac-SIG] Extending Python (OS 10.3.9)

Kevin J. MacKenzie kjmacken at gmail.com
Fri Sep 30 21:12:22 CEST 2005


Hi everyone,

I am attempting to convert some time critical code into C to be imported as
a module into python.  This is on OS X (10.3.9) using the bundled 2.3.3
version of python.

I am working through the extending python tutorial but am having trouble
wrapping my head around some things which are a little above my knowledge of
C and OS X.

Full code is copied below.  But, basically what I've done is created 2
source files, mod.c and sum.c with a header file sum.h (as per the
tutorial).

For compiling, I am doing the following:

[code]
[xxxxxxx at viridian 14:03:21]$ gcc $PYTHPATH sum.c -o sum.o
[/code]

Where $PYTHPATH 
='-I/System/Library/Frameworks/Python.framework/Versions/2.3/include/python2
.3'
So gcc can find python.h

sum.c compiles fine.  The wrapper file mod.c, however gives me the
following.

[code]
[xxxxxxx at viridian 14:03:12]$ gcc $PYTHPATH mod.c -o mod.o
ld: Undefined symbols:
_main
_PyArg_ParseTuple
_Py_BuildValue
_Py_InitModule4
_sum
[/code]

I was under the impression that I should compile the source files, and then
compile a shared library by the following:

[xxxxxx at viridian 14:03:14]$ gcc $PYTHPATH mod.o sum.o -o mod.so

However, the compile is pooching out on the compile of mod.c

I am hoping that someone with more experience with this may give me some
tips regarding how to go about this.  The code from the tutorial I have
pasted below.

Thanks.

kjm




// ------------------------------------------------------------
// begin sum.c

#include "sum.h"

int sum(int a, int b)
{
        return a + b;
}

int main(int argc, char* argv[])
{
        return 0;
}

//----------------------------------------------------------

//-----------------------------------------------------------
// being sum.h
int sum(int a, int b);
int main(int argc, char* argv[]);
//-----------------------------------------------------------

//-----------------------------------------------------------
// being mod.c
#undef _DEBUG
#include <Python.h>
#include "sum.h"

static PyObject* mod_sum(PyObject *self, PyObject *args)
{
    int a;
    int b;
    int s;
    if (!PyArg_ParseTuple(args,"ii",&a,&b))
       return NULL;
    s = sum(a,b);
    return Py_BuildValue("i",s);
}

static PyMethodDef ModMethods[] = {
    {"sum", mod_sum, METH_VARARGS, "Description.."},
    {NULL,NULL,0,NULL}
};

PyMODINIT_FUNC initmod(void)
{
    PyObject *m;
    m = Py_InitModule("mod",ModMethods);
    if (m == NULL)
       return;
}








More information about the Pythonmac-SIG mailing list