Extending Python under Cygwin

Dear Sir/Madam, My name is Oleksandr Zhmudskyy and I am working at Tulane University (Center for Computational Science). 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 Lutz's book (p, 1098-1100): Mark Lutz. Programming Python, O'REILLY. Second Edition. At the bottom of this message you can see these programs for your convenience. 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 steps: 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 `message':/home/AAZH/MarkLutz/hello.c:14: undefined reference to `PyArg_Parse' hello.o(.text+0x4a):/home/AAZH/MarkLutz/hello.c:17: undefined reference to `strcpy' hello.o(.text+0x5c):/home/AAZH/MarkLutz/hello.c:18: undefined reference to `strcat' hello.o(.text+0x6f):/home/AAZH/MarkLutz/hello.c:19: undefined reference to `Py_BuildValue' hello.o(.text+0xb8): In function `inithello':/home/AAZH/MarkLutz/hello.c:32: undefined reference to `Py_InitModule4' Note that simple GUI applications that don't need Python Extending are working correctly on Cygwin (for example, hello.py or some programs like this). In general we don't need any paths like -I/usr/local/include/python2.2 or -I/usr/include/python2.3/ because they are already installed by Python. I checked this. Also on STI Python we can see this directly:
import sys sys.path ['', '/usr/local/opt/Python-2.2.3/lib/python2.2', '/usr/local/opt/Python-2.2.3/lib/python2.2/plat-irix646', '/usr/local/opt/Python-2.2.3/lib/python2.2/lib-tk', '/usr/local/opt/Python-2.2.3/lib/python2.2/lib-dynload', '/usr/local/opt/Python-2.2.3/lib/python2.2/site-packages']
And the same command on Cygwin (PC):
import sys sys.path ['', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-cygwin', '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload', '/usr/lib/python2.3/site-packages']
Would you be so kind to help me? The thing is that installation Python on Cygwin was done correctly: no mistakes. Thank you beforehand. Sincerely, 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 */ } ****************************************

Oleksandr writes:
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 Lutz's book (p, 1098-1100): Mark Lutz. Programming Python, O'REILLY. Second Edition. At the bottom of this message you can see these programs for your convenience. 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 steps:
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
use gcc -shared instead of ld you will also need to link with the Python Library as Win32 does not allow undefined symbols For example using your test file < named as hello_py,c > <550> tmp $ gcc -shared -o hello.dll hello_py.c -I/usr/include/Python2.2 /lib/python2.2/config/libpython2.2.dll.a <551> tmp $ ls *.dll hello.dll <552> tmp $ python Python 2.2.3 (#1, Jun 8 2003, 14:58:23) [GCC 3.2 20020927 (prerelease)] on cygwin Type "help", "copyright", "credits" or "license" for more information.
import hello dir(hello) ['__doc__', '__file__', '__name__', 'message'] hello.message <built-in function message> hello.message("Hi There") 'Hello, Hi There'
HTH Norman

Oleksandr, On Tue, Aug 26, 2003 at 08:47:42AM -0500, Oleksandr wrote:
Would you be so kind to help me?
With the attached patch (first attachment), your example can be built as follows: $ gcc -shared -I /usr/include/python2.3 -o hello.dll hello.c -L /usr/lib/python2.3/config -lpython2.3 But, why not use Distutils which will do the "heavy lifting" for you? See the attached setup.py (second attachment). Using this script, your example can be built as follows: $ python setup.py build running build running build_ext building 'hello' extension creating build creating build/temp.cygwin-1.5.2-i686-2.3 gcc -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I/usr/include/python2.3 -c hello.c -o build/temp.cygwin-1.5.2-i686-2.3/hello.o creating build/lib.cygwin-1.5.2-i686-2.3 gcc -shared -Wl,--enable-auto-image-base build/temp.cygwin-1.5.2-i686-2.3/hello.o -L/usr/lib/python2.3/config -lpython2.3 -o build/lib.cygwin-1.5.2-i686-2.3/hello.dll Jason -- PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6
participants (3)
-
Jason Tishler
-
Norman Vine
-
Oleksandr