How to link a C extension module on Mac OS X?

Discussion in 'Python' started by Fortepianissimo, Jul 23, 2003.

  1. Just started learning how to write a C extension module on Mac OS X.
    Here is a simple module taken from Programming Python:

    ---
    #include <Python.h>
    #include <string.h>

    /* module functions */
    static PyObject * /* returns object */
    message(PyObject *self, PyObject *args) /* self unused in
    modules */
    { /* args from python
    call */
    char *fromPython, result[64];
    if (! PyArg_Parse(args, "(s)", &fromPython)) /* convert Python ->
    C */
    return NULL; /* null=raise
    exception */
    else {
    strcpy(result, "Hello, "); /* build up C string
    */
    strcat(result, fromPython); /* add passed Python
    string */
    return Py_BuildValue("s", result); /* convert C ->
    Python */
    }
    }

    /* registration table */
    static struct PyMethodDef hello_methods[] = {
    {"message", message, 1}, /* method name, C func ptr,
    always-tuple */
    {NULL, NULL} /* end of table marker */
    };
    ,
    /* module initializer */
    void inithello( ) /* called on first import */
    { /* name matters if loaded
    dynamically */
    (void) Py_InitModule("hello", hello_methods); /* mod name, table
    ptr */
    }
    ---

    Then I did this

    g++ -I/sw/include/python2.3 -dynamiclib -o hello.dylib hello.c

    and got this error message:

    In file included from /sw/include/python2.3/Python.h:70,
    from hello.c:6:
    /sw/include/python2.3/objimpl.h:255: warning: use of `long double'
    type; its
    size may change in a future release
    /sw/include/python2.3/objimpl.h:255: warning: (Long double usage is
    reported
    only once for each file.
    /sw/include/python2.3/objimpl.h:255: warning: To disable this warning,
    use
    -Wno-long-double.)
    ld: Undefined symbols:
    _PyArg_Parse
    _Py_BuildValue
    _Py_InitModule4
    /usr/bin/libtool: internal link edit command failed



    This is Mac OS X 10.2.6 with latest Fink installed. I guess the fetal
    one is the ld reporting undefined symbols. Any tip? Thx.
     
    Fortepianissimo, Jul 23, 2003
    #1
    1. Advertising

  2. Fortepianissimo wrote:
    > Just started learning how to write a C extension module on Mac OS X.
    > Here is a simple module taken from Programming Python: [...]
    > ld: Undefined symbols:
    > _PyArg_Parse
    > _Py_BuildValue
    > _Py_InitModule4
    > /usr/bin/libtool: internal link edit command failed
    >
    > This is Mac OS X 10.2.6 with latest Fink installed. I guess the fetal
    > one is the ld reporting undefined symbols. Any tip? Thx.


    Two words: Use distutils.

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jul 23, 2003
    #2
    1. Advertising

  3. Gerhard Häring wrote:
    > Two words: Use distutils.


    And it turns out that, rather unintuitively, the way distutils
    does it is that it *doesn't* try to create a dynamic library,
    just an ordinary object file named with a .so suffix...

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing (using news.cis.dfn.de), Jul 24, 2003
    #3
  4. Michael Hudson wrote:
    > It works. Don't know why, mind -- the files end .so when shared
    > libraries usually end .dylib on OS X -- but it does.


    I think Darwin is using something like the original BSD
    model for shared libraries, in which the .so was really
    just a naming convention, and any object file could be
    loaded at run time (albeit with a possible performance
    penalty if it wasn't designed for it).

    Although Darwin seems to have a couple of different
    flavours of dynamic linking. When the OS X docs talk about
    a "dynamically linked" library, they seem to be referring
    to a mechanism which defers resolving references to functions
    until the first time they're called. It appears that it's
    possible for code to be loaded at run time without being
    "dynamically linked" in that sense.

    It probably helps that Python is explicitly loading the
    code, in which case it probably doesn't matter what the
    filename is.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing (using news.cis.dfn.de), Jul 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    903
    Kevin Spencer
    May 17, 2005
  2. Anand
    Replies:
    3
    Views:
    914
    Tim Daneliuk
    Nov 8, 2003
  3. Ilariu Raducan
    Replies:
    2
    Views:
    357
    Ilariu Raducan
    Jul 14, 2004
  4. Simon Burton
    Replies:
    3
    Views:
    296
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Nov 23, 2005
  5. Floris Bruynooghe
    Replies:
    1
    Views:
    361
    Floris Bruynooghe
    Dec 24, 2008
Loading...

Share This Page