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