[C++-sig] Question about linking

Oleksandr ozhmuds at tulane.edu
Fri Aug 22 23:44:30 CEST 2003

Dear Marc,

My name is Oleksandr. I have found your letter "Problem building
boost.python for Cygwin".  As I think my problem is close to yours.  
I have a problem with Extending Python when I try to use GNU software.  
I installed Cygwin shell on PC and try to repeat example from Mark
Lutz's book (p, 
Mark Lutz. Programming Python, O'REILLY.  Second  Edition.
At the bottom of this message you can see these programs for your
The only difference is that path to the Python folder is changed.  
This example is working Ok on Unix machine (IRIX 6.5) after the next two

1. gcc -g -c hello.c -I/usr/local/include/python2.2 -o hello.o
2. ld -shared hello.o -o hello.so

 Here I deliberately use command line (not Makefile) in order to
simplify the 
question.   At the first step file hello.o is created and on the second
one file 
hello.so. And everything is working.

Under the Cygwin shell on PC the first step is Ok:
 gcc hello.c -g -c -I/usr/include/python2.3/ -shared -o hello.o

And I have object file hello.o but the second step gives me mistakes
like this:

$ ld hello.o -g  -I/usr/include/python2.3/  -shared -o hello.so

hello.o(.text+0x2a): In function
undefined reference to `PyArg_Parse'
hello.o(.text+0x4a):/home/AAZH/MarkLutz/hello.c:17: undefined reference
hello.o(.text+0x5c):/home/AAZH/MarkLutz/hello.c:18: undefined reference
hello.o(.text+0x6f):/home/AAZH/MarkLutz/hello.c:19: undefined reference
hello.o(.text+0xb8): In function
undefined reference to `Py_InitModule4'

I checked that on PC folder /usr/include/python2.3/ contains the whole
list of 
all hider files and the function names are in the corresponding files. 
Also I 
spend a lot of time trying to solve this problem but ...
I hope that you know the answer.  Please let me know if so.  Thank you

O. Zhmudsky

import hello

print hello.message('C')
print hello.message('module ' + hello.__file__)

for i in range(3):
    print hello.message(str(i))

#include </usr/include/python2.3/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 */

More information about the Cplusplus-sig mailing list