472,960 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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 3651
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Hunter | last post by:
I am using distutils to build a python extension module on win32. I initialize the Extension class with a list of libraries with Extension("_gd", , include_dirs=incdirs, library_dirs=libdirs,...
7
by: Carl Waldbieser | last post by:
I tried to adapt the instructions for building the M2Crypto module (http://sandbox.rulemaker.net/ngps/m2/INSTALL.html) to build a version compatible with Python2.3, but I've had some mixed results....
1
by: Edward C. Jones | last post by:
I compile and link Python extension modules using the script gcc -fPIC -g -I/usr/local/include/python2.3 \ -Wall -Wstrict-prototypes -c mymodule.c g++ -shared mymodule.o -L/usr/local/lib -o...
8
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. This module does some work on some own data types that consist of some values. The functions that can change the data are written in C. ...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
10
by: Java and Swing | last post by:
I need to write an extension for a C function so that I can call it from python. C code (myapp.c) ====== typedef unsigned long MY_LONG; char *DoStuff(char *input, MY_LONG *x) { ... } so...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
5
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)?...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.