swig cygwin python

Jason Tishler jason at tishler.net
Tue Sep 4 22:08:14 EDT 2001


Marcin,

On Tue, Sep 04, 2001 at 05:01:11PM -0700, Marcin Matuszkiewicz wrote:
> I am using swig to generate a wrapper around C functions I want to
> call from python.  I am using gcc2.95.3-5 (cygwin special) and python
> 2.1 that come with cygwin.  I have a linker problem when creating .so
> file.  Here are the details.
> $ swig -python example.i
> Generating wrappers for Python
> $ gcc -c example.c
> $ gcc -I/usr/include/python2.1 -c example_wrap.c
> $ gcc -shared example.o example_wrap.o -L/lib/python2.1/config -lpython2.1.dll
> example_wrap.o(.text+0x1e6):example_wrap.c: undefined reference to
> `PyExc_NameError'
> [snip]
> collect2: ld returned 1 exit status
> $ nm /lib/python2.1/config/libpython2.1.dll.a | grep NameError
> 00000000 I __imp__PyExc_NameError
> Does anyone know what is going on and/or how to fix it?

Yes, welcome to the wacky world of Windows DLLs -- __declspec(dllimport),
__declspec(dllexport), and whatnot.

The solution is to add "-DUSE_DL_IMPORT" when compiling Cygwin Python
shared extension source files:

    $ gcc -DUSE_DL_IMPORT -I/usr/include/python2.1 -c example_wrap.c

and to link against the Cygwin Python DLL's import library instead of
the actual DLL:

    $ gcc -shared -Wl,--enable-auto-image-base example.o example_wrap.o -L/usr/lib/python2.1/config -lpython2.1

BTW, if you can use Distutils or Misc/Makefile.pre.in to build your
extension, then the above will happen automatically.

Jason




More information about the Python-list mailing list