473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3673
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.