summaryrefslogtreecommitdiff
path: root/Examples/python/index.html
blob: 750c0f04ae0f7efb6ec2d290f0263d4fefd9de56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<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
&gt;&gt;&gt; import interface
&gt;&gt;&gt; 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>

For Python 3, set the environment variable <tt>PY3=1</tt>.

<p>
Your mileage may vary.  If you experience a problem, please let us know by 
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
</body>
</html>