Creating Python extensions in C/C++ with SWIG on windows


This  is a cookbook for creating Python extensions in C/C++ under Windows with SWIG, distutils and gcc (MinGW version).
I am using to the following versions

1 . SWIG Version 1.3.24

2 . Python 2.3.4

3.mingw 3.0.0

1. Get and install MinGW gcc
Download the compiler from This GCC compiler runs under Windows and compiled programs do not require support DLL like CygWin GCC.

You only need to download MinGW-1.1.tar.gz (roughly 10,6 Mb). It contains the whole compiler, support utilities, documentation, librairies and header files.

Once decompressed, you should add the \bin directory of MinGW to your path environment variable.
(Example : Under Windows 95/98/ME, if you installed MinGW to c:\gcc, you would add SET PATH=c:\gcc\bin;%PATH% to your AUTOEXEC.BAT.)

If installed properly, you should be able to run gcc –version anywhere. (Mine displays : 2.95.3-6).

2 . Get and install Python
Download the executable from . As usually set the path to run python from anywhere.

3 . Create libpython23.a
To create Python extensions, you need to link against the Python library. Unfortunately, most Python distributions are provided with Python23.lib, a library in Microsoft Visual C++ format. GCC expects a .a file (libpython23.a to be precise.). Here’s how to convert python23.lib to libpython23.a:

Download pexport (from here or
Get Python23.dll (it should be somewhere on your harddrive).
Run : pexports python23.dll > python23.def
This will extract all symbols from python23.dll and write them into python23.def.
Run : dlltool –dllname python23.dll –def python23.def –output-lib libpython23.a
This will create libpython23.a (dlltool is part of MinGW utilities).
Copy libpython23.a to c:\python23\libs\ (in the same directory as python23.lib).
This trick should work for all Python versions, including future releases of Python. You can also use this trick to convert other libraries.

4. Get and install SWIG
SWIG is a wrapper for C/C++ sources. It allows you to use C/C++ functions and classes to Python with a minimum effort.

Download SWIG binaries for Window ( Swigwin ) from, decompress them and add swig directory to your path (the directory where swig.exe is located).

5 . Write .i files
Write the .i files corresponding to your c++ files ( for more on how to write these files visit )

6 . Use the following commands to get it going

A . swig –python –c++ example.i

output: & example_wrap.cxx
B. g++ -c *.c++

output: .o files corresponding to your C++
C. g++ -c example_wrap.cxx -Ic:\python23\include

output: example_wrap.o
D. g++ -shared *.o -o _example.pyd -Lc:\python23\libs -lpython23

output: _example.pyd

Hopefully this worked . Now you can do an “ import example “ in python .


5 Responses to “Creating Python extensions in C/C++ with SWIG on windows”

  1. 1 mc wong

    Here are the actual steps on WinXP SP2

    pexports pythoncom25.dll > python25.def

    comments : PExports 0.43 Copyright 1998, Anders Norlander
    Changed 1999, Paul Sokolovsky

    dlltool -k –input-def python25.def –dllname pythoncom25.dll –output-lib libpython25.a

    comments : GNU dlltool (GNU Binutils) 2.19.1
    this step differs from your given example (maybe different versions of dlltool?)

    swig –python –c++ example.i

    g++ -c *.c
    comments : seems “g++ -c *.c++” will output errors as only .c generated or needs to be renamed.
    I’m using g++ (GCC) 3.4.5 (mingw-vista special r3)

    g++ -c example_wrap.cxx -Ic:\python25\include
    comments : i have python25 installed

    g++ -shared *.o -o _example.pyd -Lc:\python25\libs -lpython25

    g++ -c example_wrap.cxx -Ic:\python25\include

    g++ -shared *.o -o _example.pyd -Lc:\python25\libs -lpython25

    All objects created.
    09/30/2009 10:41 AM 311 example.c
    09/30/2009 10:42 AM 347 example.i
    09/30/2009 02:09 PM 596 example.o
    09/30/2009 02:08 PM 2,502
    09/30/2009 02:08 PM 109,072 example_wrap.cxx
    09/30/2009 02:11 PM 33,279 example_wrap.o
    09/30/2009 02:06 PM 368,484 libpython25.a
    09/30/2009 02:05 PM 20,188 python25.def
    12/23/2008 03:10 PM 199,524 python25.lib
    07/08/2009 11:50 PM 339,968 pythoncom25.dll
    09/30/2009 02:13 PM 60,166 _example.pyd

  2. 2 Staffan

    Thank you so much for this guide! It’s super helpful. As mentioned by MC Wong, I did have to change the dlltool command slightly,, mine was simply

    dlltool -D python27.dll -d python27.def -l libpython27.a

    I think in particular “-def” does not use.

    I also wonder about the .i files for Swig, do you know what variables you should include in there? All that are used in the C++ file or only the ones you want to be able to access? I am quite confused by it.

  3. I always used to read post in news papers but now as I am a user of net
    so from now I am using net for articles,
    thanks to web.

  4. Reblogged this on Lavender Turquois.

  5. I have been told to improve your creative writing you need to read a lot.. What great classic books, can I read (from any period or country) that will improve my writing.. Genre is unimportant, though I am not writing for children.. I said classic, I read science fiction/recent literature as entertainment. But to improve the quality of writing, read the masters. Can any one write a list of great writers to read..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: