| <html> | |
| <head> | |
| <title>SWIG:Examples:python</title> | |
| </head> | |
| <body bgcolor="#ffffff"> | |
| <H1>SWIG Python Examples</H1> | |
| <p> | |
| The following examples illustrate the use of SWIG with Python. | |
| <ul> | |
| <li><a href="simple/index.html">simple</a>. A minimal example showing how SWIG can | |
| be used to wrap a C function, a global variable, and a constant. | |
| <li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and | |
| certain C declarations are turned into constants. | |
| <li><a href="variables/index.html">variables</a>. An example showing how to access C global variables from Python. | |
| <li><a href="value/index.html">value</a>. How to pass and return structures by value. | |
| <li><a href="class/index.html">class</a>. Wrapping a simple C++ class. | |
| <li><a href="reference/index.html">reference</a>. C++ references. | |
| <li><a href="pointer/index.html">pointer</a>. Simple pointer handling. | |
| <li><a href="funcptr/index.html">funcptr</a>. Pointers to functions. | |
| </ul> | |
| <h2>Compilation Issues</h2> | |
| <ul> | |
| <li>To create a Python extension, SWIG is run with the following options: | |
| <blockquote> | |
| <pre> | |
| % swig -python interface.i | |
| </pre> | |
| </blockquote> | |
| <li> | |
| Please see the <a href="../../Doc/Manual/Windows.html">Windows</a> page in the main manual for information on using the examples on Windows. <p> | |
| </li> | |
| <li>On Unix the compilation of examples is done using the file <tt>Example/Makefile</tt>. This | |
| makefile performs a manual module compilation which is platform specific. Typically, | |
| the steps look like this (Linux): | |
| <blockquote> | |
| <pre> | |
| % swig -python interface.i | |
| % gcc -fPIC -c interface_wrap.c -I/usr/local/include/python1.5 | |
| % gcc -shared interface_wrap.o $(OBJS) -o interfacemodule.so | |
| % python | |
| Python 1.5.2 (#3, Oct 9 1999, 22:09:34) [GCC 2.95.1 19990816 (release)] on linux2 | |
| Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam | |
| >>> import interface | |
| >>> interface.blah(...) | |
| ... | |
| </pre> | |
| </blockquote> | |
| <li>The politically "correct" way to compile a Python extension is to follow the steps | |
| described at <a href="http://docs.python.org/2.0/ext/building-on-unix.html">www.python.org</a>: | |
| <p> | |
| <ol> | |
| <li>Create a file called <tt>Setup</tt> that looks like the following where $(SRCS) is filled | |
| in with any other source files you need to build the extension: | |
| <blockquote> | |
| <pre> | |
| *shared* | |
| interface interface_wrap.c $(SRCS) | |
| </pre> | |
| </blockquote> | |
| <li>Copy the file <tt>Makefile.pre.in</tt> from the Python distribution. Usually it's located | |
| in the directory <tt>/usr/local/lib/python1.5/config</tt> on a Unix machine. | |
| <p> | |
| <li>Type the following to build the extension: | |
| <blockquote> | |
| <pre> | |
| % make -f Makefile.pre.in boot | |
| % make | |
| </pre> | |
| </blockquote> | |
| <li> And that's it. If you are preparing an extension for distribution, you may want | |
| to look at the <a href="http://www.python.org/sigs/distutils-sig/">distutils</a>. | |
| </ol> | |
| </ul> | |
| <h2>Compatibility</h2> | |
| <p> | |
| Your mileage may vary. If you experience a problem, please let us know by | |
| contacting us on the <a href="https://www.swig.org/mail.html">mailing lists</a>. | |
| </body> | |
| </html> | |