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

F

Fortepianissimo

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.
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Fortepianissimo said:
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
 
G

Greg Ewing (using news.cis.dfn.de)

Gerhard said:
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...
 
G

Greg Ewing (using news.cis.dfn.de)

Michael said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top