Using SWIG to build Python extensions -- help!

Carl Bevil carl_bevil at
Fri Jan 24 21:19:59 CET 2003

Hello all.  I'm experimenting with using SWIG to build a Python extension.  I 
have a really simple C++ file that I'd like to extend Python with as a test.

I'm building the interpreter as a library that I statically link into my main 
application.  Dynamic linking is not an option for my target platform, so I've 
downloaded the Python source code.  I'm using Python 2.1.3.

I create two files, swigtest.cpp and swigtest.h.  swigtest.h has a function 
prototype, and swigtest.cpp has the function body.

Then I run SWIG:

swig -python -c++ -module swigtest swigtest.h

This builds two files: and swigtest_wrap.cxx.  I rename the latter 
to swigtest.cpp, as the compiler I'm using (Metrowerks) doesn't like .cxx files.

Then I compile and link swigtest_wrap.cpp and swigtest.cpp into the python 
interpreter, creating a python21_d.lib file (I'm on Windows).

Then I build my main application (a C++ app), and link it statically with the 
python interpreter library that I just built.

When I run, I execute the following statement:

PyRun_SimpleString("import swigtest");

.. and I get an error that there is no module named swigtest.

What am I doing wrong here?  And do I need to do anything with the 
file that SWIG creates? I've read the chapter in O'Reilly's Programming Python 
on extending Python, but it makes no mention of SWIG generating a .py file.  
And all of its examples are dynamically linked.



More information about the Python-list mailing list