4
4
<title>Working with Modules</title>
5
<link rel="stylesheet" type="text/css" href="style.css">
7
8
<body bgcolor="#ffffff">
8
9
<H1><a name="Modules"></a>15 Working with Modules</H1>
11
<div class="sectiontoc">
11
13
<li><a href="#Modules_nn2">The SWIG runtime code</a>
12
<li><a href="#Modules_nn3">A word of caution about static libraries</a>
13
<li><a href="#Modules_nn4">References</a>
14
<li><a href="#Modules_nn5">Reducing the wrapper file size</a>
14
<li><a href="#external_run_time">External access to the runtime</a>
15
<li><a href="#Modules_nn4">A word of caution about static libraries</a>
16
<li><a href="#Modules_nn5">References</a>
17
<li><a href="#Modules_nn6">Reducing the wrapper file size</a>
20
25
When first working with SWIG, users commonly start by creating a
21
26
single module. That is, you might define a single SWIG interface that
22
27
wraps some set of C/C++ code. You then compile all of the generated
68
76
languages provide. One solution is to load all modules before spawning any threads.
71
<H2><a name="Modules_nn3"></a>15.2 A word of caution about static libraries</H2>
79
<H2><a name="external_run_time"></a>15.2 External access to the runtime</H2>
82
<p>As described in <a href="Typemaps.html#runtime_type_checker">The run-time type checker</a>,
83
the functions <tt>SWIG_TypeQuery</tt>, <tt>SWIG_NewPointerObj</tt>, and others sometimes need
84
to be called. Calling these functions from a typemap is supported, since the typemap code
85
is embedded into the <tt>_wrap.c</tt> file, which has those declerations available. If you need
86
to call the SWIG run-time functions from another C file, there is one header you need
87
to include. To generate the header that needs to be included, run the following command:
89
<div class="code"><pre>
90
$ swig -python -external-runtime <filename>
93
<p>The filename argument is optional and if it is not passed, then the default filename will
94
be something like <tt>swigpyrun.h</tt>, depending on the language. This header file should
95
be treated like any of the other _wrap.c output files, and should be regenerated when the
96
_wrap files are. After including this header, your code will be able to call <tt>SWIG_TypeQuery</tt>,
97
<tt>SWIG_NewPointerObj</tt>, <tt>SWIG_ConvertPtr</tt> and others. The exact argument paramaters
98
for these functions might differ between language modules; please check the language module chapters
99
for more information.</p>
101
<p>Inside this header the functions are declared static and are included inline into the file,
102
and thus the file does not need to be linked against any SWIG libraries or code (you might still
103
need to link against the language libraries like libpython-2.3). Data is shared between this
104
file and the _wrap.c files through a global variable in the scripting language. It is also
105
possible to copy this header file along with the generated wrapper files into your own package,
106
so that you can distribute a package that can be compiled without SWIG installed (this works
107
because the header file is self contained, and does not need to link with anything).</p>
109
<H2><a name="Modules_nn4"></a>15.3 A word of caution about static libraries</H2>
74
113
When working with multiple SWIG modules, you should take care not to use static
75
114
libraries. For example, if you have a static library <tt>libfoo.a</tt> and you link a collection
76
115
of SWIG modules with that library, each module will get its own private copy of the library code inserted
77
116
into it. This is very often <b>NOT</b> what you want and it can lead to unexpected or bizarre program
78
117
behavior. When working with dynamically loadable modules, you should try to work exclusively with shared libaries.
80
<H2><a name="Modules_nn4"></a>15.3 References</H2>
120
<H2><a name="Modules_nn5"></a>15.4 References</H2>
83
124
Due to the complexity of working with shared libraries and multiple modules, it might be a good idea to consult
84
125
an outside reference. John Levine's "Linkers and Loaders" is highly recommended.
86
<H2><a name="Modules_nn5"></a>15.4 Reducing the wrapper file size</H2>
128
<H2><a name="Modules_nn6"></a>15.5 Reducing the wrapper file size</H2>