Building Python Extensions With ActivePython 2.5 And Visual Studio 2005

Actually, the title should be “you shouldn’t bother trying to build python extensions with Visual Studio 2005″. All sorts of complications arise and I haven’t got it working at all. Here’s some references if you enjoy a little self-flaggelation.

1) Make sure you understand manifests. Building DLLs will never be the same again! Richard Grimes has an unflattering write up on MSVCR start up errors.

2) Try and see if DISTUTILS_USE_SDK works or not (It doesn’t with Visual Studio 2005)

3) Checkout this mailing thread Python and MSVC 8.

Here’s the easy way out:

1) Install ActivePython
2) Install Cygwin, then select GCC and MINGW
3) Grab pexports

pexports C:\\Windows\\System32\\python25.dll > python25.def
dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a
cp libpython25.a /usr/lib/.

Here’s the trickiest bit:
When you call python setup.py build -c mingw32, the linker will fail.

Here’s an example:

C:\cygwin\bin\gcc.exe -mno-cygwin -shared
-s build\temp.win32-2.5\Release\mercurial\base85.o build\temp.win32-2.5\Release\mercurial\base85.def
-LC:\python25\libs
-LC:\python25\PCbuild
-lpython25 -lmsvcr71
-o build\lib.win32-2.5\mercurial\base85.pyd
build\temp.win32-2.5\Release\mercurial\base85.o:base85.c:(.text+0x22e): undefined reference to `__imp__PyExc_ValueError'

Manually run the following instead:

C:\cygwin\bin\gcc.exe -mno-cygwin -shared
-s build\temp.win32-2.5\Release\mercurial\base85.o build\temp.win32-2.5\Release\mercurial\base85.def
-LC:\python25\libs
-LC:\python25\PCbuild
-lpython25 -lmsvcr71
-o build\lib.win32-2.5\mercurial\base85.pyd
build\temp.win32-2.5\Release\mercurial\base85.o:base85.c:(.text+0x22e): undefined reference to `__imp__PyExc_ValueError'

Manually build the DLL this way:

$ gcc -mno-cygwin -mdll build/temp.win32-2.5/Release/mercurial/base85.o
build/temp.win32-2.5/Release/mercurial/base85.def
-lpython25 -lmsvcr71 -o build/lib.win32-2.5/mercurial/base85.pyd

To be honest, I don’t know why -shared is broken in my version of ld

$ ld -v
GNU ld version 2.17.50 20060817

Hope this helps you out.

ps. If you find this article useful, please provide some link love. (i.e. link to it)

Bookmark and Share

You should follow me on twitter here

Leave a Reply