468,765 Members | 955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

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

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.
Jul 18 '05 #1
3 3416
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

Jul 18 '05 #2
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

Jul 18 '05 #3
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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Hunter | last post: by
7 posts views Thread by Carl Waldbieser | last post: by
1 post views Thread by Edward C. Jones | last post: by
8 posts views Thread by Torsten Mohr | last post: by
reply views Thread by Mark English | last post: by
10 posts views Thread by Java and Swing | last post: by
5 posts views Thread by Adam Atlas | last post: by
5 posts views Thread by Chuck Anderson | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.