PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Doc/Manual/Introduction.html

#
HTML | 463 lines | 382 code | 79 blank | 2 comment | 0 complexity | 50c1a175435e34e12ae7a6291c0efcf6 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Introduction</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body bgcolor="#ffffff">
  8. <H1><a name="Introduction"></a>2 Introduction</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#Introduction_nn2">What is SWIG?</a>
  13. <li><a href="#Introduction_nn3">Why use SWIG?</a>
  14. <li><a href="#Introduction_nn4">A SWIG example</a>
  15. <ul>
  16. <li><a href="#Introduction_nn5">SWIG interface file</a>
  17. <li><a href="#Introduction_nn6">The swig command</a>
  18. <li><a href="#Introduction_nn7">Building a Perl5 module</a>
  19. <li><a href="#Introduction_nn8">Building a Python module</a>
  20. <li><a href="#Introduction_nn9">Shortcuts</a>
  21. </ul>
  22. <li><a href="#Introduction_nn10">Supported C/C++ language features</a>
  23. <li><a href="#Introduction_nn11">Non-intrusive interface building</a>
  24. <li><a href="#Introduction_build_system">Incorporating SWIG into a build system</a>
  25. <li><a href="#Introduction_nn12">Hands off code generation</a>
  26. <li><a href="#Introduction_nn13">SWIG and freedom</a>
  27. </ul>
  28. </div>
  29. <!-- INDEX -->
  30. <H2><a name="Introduction_nn2"></a>2.1 What is SWIG?</H2>
  31. <p>
  32. SWIG is a software development tool that simplifies the task of
  33. interfacing different languages to C and C++ programs. In a
  34. nutshell, SWIG is a compiler that takes C/C++ declarations and creates
  35. the wrappers needed to access those declarations from other languages including
  36. including Perl, Python, Tcl, Ruby, Guile, and Java. SWIG normally
  37. requires no modifications to existing code and can often be used to
  38. build a usable interface in only a few minutes. Possible applications
  39. of SWIG include:
  40. </p>
  41. <ul>
  42. <li>Building interpreted interfaces to existing C programs.
  43. <li>Rapid prototyping and application development.
  44. <li>Interactive debugging.
  45. <li>Reengineering or refactoring of legacy software into a scripting language components.
  46. <li>Making a graphical user interface (using Tk for example).
  47. <li>Testing of C libraries and programs (using scripts).
  48. <li>Building high performance C modules for scripting languages.
  49. <li>Making C programming more enjoyable (or tolerable depending on your point of view).
  50. <li>Impressing your friends.
  51. <li>Obtaining vast sums of research funding (although obviously not applicable to the author).
  52. </ul>
  53. <p>
  54. SWIG was originally designed to make it extremely easy for scientists
  55. and engineers to build extensible scientific software without having to get a
  56. degree in software engineering. Because of this, the use of
  57. SWIG tends to be somewhat informal and ad-hoc (e.g., SWIG does not
  58. require users to provide formal interface specifications as you would find in
  59. a dedicated IDL compiler). Although
  60. this style of development isn't appropriate for every
  61. project, it is particularly well suited to software development in the
  62. small; especially the research and development work that is commonly found
  63. in scientific and engineering projects. However, nowadays SWIG is known to be used in many
  64. large open source and commercial projects.
  65. <H2><a name="Introduction_nn3"></a>2.2 Why use SWIG?</H2>
  66. <p>
  67. As stated in the previous section, the primary purpose of SWIG is to simplify
  68. the task of integrating C/C++ with other programming languages. However, why would
  69. anyone want to do that? To answer that question, it is useful to list a few strengths
  70. of C/C++ programming:
  71. </p>
  72. <ul>
  73. <li>Excellent support for writing programming libraries.
  74. <li>High performance (number crunching, data processing, graphics, etc.).
  75. <li>Systems programming and systems integration.
  76. <li>Large user community and software base.
  77. </ul>
  78. <p>
  79. Next, let's list a few problems with C/C++ programming
  80. </p>
  81. <ul>
  82. <li>Writing a user interface is rather painful (i.e., consider programming with MFC, X11, GTK, or any number
  83. of other libraries).
  84. <li>Testing is time consuming (the compile/debug cycle).
  85. <li>Not easy to reconfigure or customize without recompilation.
  86. <li>Modularization can be tricky.
  87. <li>Security concerns (buffer overflow for instance).
  88. </ul>
  89. <p>
  90. To address these limitations, many programmers have arrived at the
  91. conclusion that it is much easier to use different programming
  92. languages for different tasks. For instance, writing a graphical user
  93. interface may be significantly easier in a scripting language like
  94. Python or Tcl (consider the reasons why millions of programmers have used languages like
  95. Visual Basic if you need more proof). An interactive interpreter might also serve as a
  96. useful debugging and testing tool. Other languages like Java might
  97. greatly simplify the task of writing distributed computing software.
  98. The key point is that different programming languages offer different
  99. strengths and weaknesses. Moreover, it is extremely unlikely that any
  100. programming is ever going to be perfect. Therefore, by combining
  101. languages together, you can utilize the best features of each language
  102. and greatly simplify certain aspects of software development.
  103. </p>
  104. <p>
  105. From the standpoint of C/C++, a lot of people use SWIG because they want to break
  106. out of the traditional monolithic C programming model which usually results
  107. in programs that resemble this:
  108. <ul>
  109. <li>A collection of functions and variables that do something useful.
  110. <li>A <tt>main()</tt> program that starts everything.
  111. <li>A horrible collection of hacks that form some kind of user interface (but
  112. which no-one really wants to touch).
  113. </ul>
  114. <p>
  115. Instead of going down that route, incorporating C/C++ into a higher level language
  116. often results in a more modular design, less code, better flexibility, and increased
  117. programmer productivity.
  118. </p>
  119. <p>
  120. SWIG tries to make the problem of C/C++ integration as painless as possible.
  121. This allows you to focus on the underlying C
  122. program and using the high-level language interface, but not
  123. the tedious and complex chore of making the two languages talk to each
  124. other. At the same time, SWIG recognizes that all applications are different. Therefore,
  125. it provides a wide variety of customization features that let you change almost
  126. every aspect of the language bindings. This is the main reason why SWIG has such a large
  127. user manual ;-).
  128. <H2><a name="Introduction_nn4"></a>2.3 A SWIG example</H2>
  129. <p>
  130. The best way to illustrate SWIG is with a simple example. Consider the
  131. following C code:
  132. </p>
  133. <div class="code"><pre>
  134. /* File : example.c */
  135. double My_variable = 3.0;
  136. /* Compute factorial of n */
  137. int fact(int n) {
  138. if (n &lt;= 1) return 1;
  139. else return n*fact(n-1);
  140. }
  141. /* Compute n mod m */
  142. int my_mod(int n, int m) {
  143. return(n % m);
  144. }
  145. </pre></div>
  146. <p>
  147. Suppose that you wanted to access these functions and the global
  148. variable <tt>My_variable</tt> from Tcl. You start by making a SWIG
  149. interface file as shown below (by convention, these files carry a .i
  150. suffix) :
  151. <H3><a name="Introduction_nn5"></a>2.3.1 SWIG interface file</H3>
  152. <div class="code"><pre>
  153. /* File : example.i */
  154. %module example
  155. %{
  156. /* Put headers and other declarations here */
  157. extern double My_variable;
  158. extern int fact(int);
  159. extern int my_mod(int n, int m);
  160. %}
  161. extern double My_variable;
  162. extern int fact(int);
  163. extern int my_mod(int n, int m);
  164. </pre></div>
  165. <p>
  166. The interface file contains ANSI C function prototypes and variable
  167. declarations. The <tt>%module</tt> directive defines the name of the
  168. module that will be created by SWIG. The <tt>%{ %}</tt> block
  169. provides a location for inserting additional code, such as C header
  170. files or additional C declarations, into the generated C wrapper code.
  171. <H3><a name="Introduction_nn6"></a>2.3.2 The swig command</H3>
  172. <p>
  173. SWIG is invoked using the <tt>swig</tt> command. We can use this to
  174. build a Tcl module (under Linux) as follows :
  175. </p>
  176. <div class="shell"><pre>
  177. unix &gt; <b>swig -tcl example.i</b>
  178. unix &gt; <b>gcc -c -fpic example.c example_wrap.c -I/usr/local/include</b>
  179. unix &gt; <b>gcc -shared example.o example_wrap.o -o example.so</b>
  180. unix &gt; <b>tclsh</b>
  181. % <b>load ./example.so</b>
  182. % <b>fact 4</b>
  183. 24
  184. % <b>my_mod 23 7</b>
  185. 2
  186. % <b>expr $My_variable + 4.5</b>
  187. 7.5
  188. %
  189. </pre></div>
  190. <p>
  191. The <tt>swig</tt> command produced a new file called
  192. <tt>example_wrap.c</tt> that should be compiled along with the
  193. <tt>example.c</tt> file. Most operating systems and scripting
  194. languages now support dynamic loading of modules. In our example, our
  195. Tcl module has been compiled into a shared library that can be loaded
  196. into Tcl. When loaded, Tcl can now access the functions
  197. and variables declared in the SWIG interface. A look at the file
  198. <tt>example_wrap.c</tt> reveals a hideous mess. However, you
  199. almost never need to worry about it.
  200. <H3><a name="Introduction_nn7"></a>2.3.3 Building a Perl5 module</H3>
  201. <p>
  202. Now, let's turn these functions into a Perl5 module. Without making
  203. any changes type the following (shown for Solaris):
  204. </p>
  205. <div class="shell"><pre>
  206. unix &gt; <b>swig -perl5 example.i</b>
  207. unix &gt; <b>gcc -c example.c example_wrap.c \
  208. -I/usr/local/lib/perl5/sun4-solaris/5.003/CORE</b>
  209. unix &gt; <b>ld -G example.o example_wrap.o -o example.so</b> # This is for Solaris
  210. unix &gt; <b>perl5.003
  211. use example;
  212. print example::fact(4), "\n";
  213. print example::my_mod(23,7), "\n";
  214. print $example::My_variable + 4.5, "\n";
  215. &lt;ctrl-d&gt;</b>
  216. 24
  217. 2
  218. 7.5
  219. unix &gt;
  220. </pre></div>
  221. <H3><a name="Introduction_nn8"></a>2.3.4 Building a Python module</H3>
  222. <p>
  223. Finally, let's build a module for Python (shown for Irix).
  224. </p>
  225. <div class="shell"><pre>
  226. unix &gt; <b>swig -python example.i</b>
  227. unix &gt; <b>gcc -c -fpic example.c example_wrap.c -I/usr/local/include/python2.0</b>
  228. unix &gt; <b>gcc -shared example.o example_wrap.o -o _example.so</b>
  229. unix &gt; <b>python</b>
  230. Python 2.0 (#6, Feb 21 2001, 13:29:45)
  231. [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
  232. Type "copyright", "credits" or "license" for more information.
  233. &gt;&gt;&gt; <b>import example</b>
  234. &gt;&gt;&gt; <b>example.fact(4)</b>
  235. 24
  236. &gt;&gt;&gt; <b>example.my_mod(23,7)</b>
  237. 2
  238. &gt;&gt;&gt; <b>example.cvar.My_variable + 4.5</b>
  239. 7.5
  240. </pre></div>
  241. <H3><a name="Introduction_nn9"></a>2.3.5 Shortcuts</H3>
  242. <p>
  243. To the truly lazy programmer, one may wonder why we needed the extra
  244. interface file at all. As it turns out, you can often do without
  245. it. For example, you could also build a Perl5 module by just running
  246. SWIG on the C header file and specifying a module name as follows
  247. </p>
  248. <div class="shell"><pre>
  249. unix &gt; <b>swig -perl5 -module example example.h</b>
  250. unix &gt; <b>gcc -c example.c example_wrap.c \
  251. -I/usr/local/lib/perl5/sun4-solaris/5.003/CORE</b>
  252. unix &gt; <b>ld -G example.o example_wrap.o -o example.so</b>
  253. unix &gt; <b>perl5.003
  254. use example;
  255. print example::fact(4), "\n";
  256. print example::my_mod(23,7), "\n";
  257. print $example::My_variable + 4.5, "\n";
  258. &lt;ctrl-d&gt;</b>
  259. 24
  260. 2
  261. 7.5
  262. </pre></div>
  263. <H2><a name="Introduction_nn10"></a>2.4 Supported C/C++ language features</H2>
  264. <p>
  265. A primary goal of the SWIG project is to make the language binding
  266. process extremely easy. Although a few simple examples have been shown,
  267. SWIG is quite capable in supporting most of C++. Some of the
  268. major features include:
  269. </p>
  270. <ul>
  271. <li>Full C99 preprocessing.
  272. <li>All ANSI C and C++ datatypes.
  273. <li>Functions, variables, and constants.
  274. <li>Classes.
  275. <li>Single and multiple inheritance.
  276. <li>Overloaded functions and methods.
  277. <li>Overloaded operators.
  278. <li>C++ templates (including member templates, specialization, and partial specialization).
  279. <li>Namespaces.
  280. <li>Variable length arguments.
  281. <li>C++ smart pointers.
  282. </ul>
  283. <p>
  284. Currently, the only major C++ feature not supported is nested classes--a limitation
  285. that should be removed in a future release, but has some workarounds for the moment.
  286. </p>
  287. <p>
  288. It is important to stress that SWIG is not a simplistic C++ lexing
  289. tool like several apparently similar wrapper generation tools. SWIG
  290. not only parses C++, it implements the full C++ type system and it is
  291. able to understand C++ semantics. SWIG generates its wrappers with
  292. full knowledge of this information. As a result, you will find SWIG
  293. to be just as capable of dealing with nasty corner cases as it is in
  294. wrapping simple C++ code. In fact, SWIG is able handle C++ code that
  295. stresses the very limits of many C++ compilers.
  296. <H2><a name="Introduction_nn11"></a>2.5 Non-intrusive interface building</H2>
  297. <p>
  298. When used as intended, SWIG requires minimal (if any) modification to
  299. existing C or C++ code. This makes SWIG extremely easy to use with existing
  300. packages and promotes software reuse and modularity. By making
  301. the C/C++ code independent of the high level interface, you can change the
  302. interface and reuse the code in other applications. It is also
  303. possible to support different types of interfaces depending on the application.
  304. </p>
  305. <H2><a name="Introduction_build_system"></a>2.6 Incorporating SWIG into a build system</H2>
  306. <p>
  307. SWIG is a command line tool and as such can be incorporated into any build system that supports invoking external tools/compilers.
  308. SWIG is most commonly invoked from within a Makefile, but is also known to be invoked from popular IDEs such as
  309. Microsoft Visual Studio.
  310. </p>
  311. <p>
  312. If you are using the GNU Autotools
  313. (<a href="http://www.gnu.org/software/autoconf/">Autoconf</a>/
  314. <a href="http://www.gnu.org/software/automake/">Automake</a>/
  315. <a href="http://www.gnu.org/software/libtool/">Libtool</a>)
  316. to configure SWIG use in your project, the SWIG Autoconf macros can be used.
  317. The primary macro is <tt>ax_pkg_swig</tt>, see
  318. <a href="http://www.gnu.org/software/autoconf-archive/ax_pkg_swig.html#ax_pkg_swig">http://www.gnu.org/software/autoconf-archive/ax_pkg_swig.html#ax_pkg_swig</a>.
  319. The <tt>ax_python_devel</tt> macro is also helpful for generating Python extensions. See the
  320. <a href="http://www.gnu.org/software/autoconf-archive/">Autoconf Archive</a>
  321. for further information on this and other Autoconf macros.
  322. </p>
  323. <p>
  324. There is growing support for SWIG in some build tools, for example <a href="http://www.cmake.org">CMake</a>
  325. is a cross-platform, open-source build manager with built in support for SWIG. CMake can detect the SWIG executable
  326. and many of the target language libraries for linking against.
  327. CMake knows how to build shared libraries and loadable modules on many different operating systems.
  328. This allows easy cross platform SWIG development. It also can generate the custom commands necessary for
  329. driving SWIG from IDE's and makefiles. All of this can be done from a single cross platform input file.
  330. The following example is a CMake input file for creating a python wrapper for the SWIG interface file, example.i:
  331. </p>
  332. <div class="code"><pre>
  333. # This is a CMake example for Python
  334. FIND_PACKAGE(SWIG REQUIRED)
  335. INCLUDE(${SWIG_USE_FILE})
  336. FIND_PACKAGE(PythonLibs)
  337. INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
  338. INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
  339. SET(CMAKE_SWIG_FLAGS "")
  340. SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
  341. SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall")
  342. SWIG_ADD_MODULE(example python example.i example.cxx)
  343. SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
  344. </pre></div>
  345. <p>
  346. The above example will generate native build files such as makefiles, nmake files and Visual Studio projects
  347. which will invoke SWIG and compile the generated C++ files into _example.so (UNIX) or _example.pyd (Windows).
  348. For other target languages on Windows a dll, instead of a .pyd file, is usually generated.
  349. </p>
  350. <H2><a name="Introduction_nn12"></a>2.7 Hands off code generation</H2>
  351. <p>
  352. SWIG is designed to produce working code that needs no
  353. hand-modification (in fact, if you look at the output, you probably
  354. won't want to modify it). You should think of your target language interface being
  355. defined entirely by the input to SWIG, not the resulting output
  356. file. While this approach may limit flexibility for hard-core hackers,
  357. it allows others to forget about the low-level implementation
  358. details.
  359. </p>
  360. <H2><a name="Introduction_nn13"></a>2.8 SWIG and freedom</H2>
  361. <p>
  362. No, this isn't a special section on the sorry state of world politics.
  363. However, it may be useful to know that SWIG was written with a
  364. certain "philosophy" about programming---namely that programmers are
  365. smart and that tools should just stay out of their way. Because of
  366. that, you will find that SWIG is extremely permissive in what it lets
  367. you get away with. In fact, you can use SWIG to go well beyond
  368. "shooting yourself in the foot" if dangerous programming is your goal.
  369. On the other hand, this kind of freedom may be exactly what is needed
  370. to work with complicated and unusual C/C++ applications.
  371. </p>
  372. <p>
  373. Ironically, the freedom that SWIG provides is countered by an
  374. extremely conservative approach to code generation. At it's core, SWIG
  375. tries to distill even the most advanced C++ code down to a small
  376. well-defined set of interface building techniques based on ANSI C
  377. programming. Because of this, you will find that SWIG interfaces can
  378. be easily compiled by virtually every C/C++ compiler and that they can
  379. be used on any platform. Again, this is an important part of staying out
  380. of the programmer's way----the last thing any developer wants to do is
  381. to spend their time debugging the output of a tool that relies on
  382. non-portable or unreliable programming features.
  383. </body>
  384. </html>