3 <title>SWIG:Examples:python</title>
6 <body bgcolor="#ffffff">
7 <H1>SWIG Python Examples</H1>
10 The following examples illustrate the use of SWIG with Python.
13 <li><a href="simple/index.html">simple</a>. A minimal example showing how SWIG can
14 be used to wrap a C function, a global variable, and a constant.
15 <li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
16 certain C declarations are turned into constants.
17 <li><a href="variables/index.html">variables</a>. An example showing how to access C global variables from Python.
18 <li><a href="value/index.html">value</a>. How to pass and return structures by value.
19 <li><a href="class/index.html">class</a>. Wrapping a simple C++ class.
20 <li><a href="reference/index.html">reference</a>. C++ references.
21 <li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
22 <li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
25 <h2>Compilation Issues</h2>
28 <li>To create a Python extension, SWIG is run with the following options:
32 % swig -python interface.i
37 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>
40 <li>On Unix the compilation of examples is done using the file <tt>Example/Makefile</tt>. This
41 makefile performs a manual module compilation which is platform specific. Typically,
42 the steps look like this (Linux):
46 % swig -python interface.i
47 % gcc -fpic -c interface_wrap.c -I/usr/local/include/python1.5
48 % gcc -shared interface_wrap.o $(OBJS) -o interfacemodule.so
50 Python 1.5.2 (#3, Oct 9 1999, 22:09:34) [GCC 2.95.1 19990816 (release)] on linux2
51 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
52 >>> import interface
53 >>> interface.blah(...)
58 <li>The politically "correct" way to compile a Python extension is to follow the steps
59 described at <a href="http://www.python.org/doc/current/ext/building-on-unix.html">www.python.org</a>
60 or in the most excellent (and shamelessly plugged) <a href="http://islab.cs.uchicago.edu/python">Python Essential Reference</a>:
64 <li>Create a file called <tt>Setup</tt> that looks like the following where $(SRCS) is filled
65 in with any other source files you need to build the extension:
70 interface interface_wrap.c $(SRCS)
73 <li>Copy the file <tt>Makefile.pre.in</tt> from the Python distribution. Usually it's located
74 in the directory <tt>/usr/local/lib/python1.5/config</tt> on a Unix machine.
77 <li>Type the following to build the extension:
81 % make -f Makefile.pre.in boot
85 <li> And that's it. If you are preparing an extension for distribution, you may want
86 to look at the <a href="http://www.python.org/sigs/distutils-sig/">distutils</a>.
90 <h2>Compatibility</h2>
92 The examples have been extensively tested on the following platforms:
99 All of the examples were last tested with the following configuration (9/1/2000):
102 <li>Sparc Solaris 2.8.
107 Your mileage may vary. If you experience a problem, please let us know by
108 contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.