PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/tcl/simple/index.html

#
HTML | 97 lines | 75 code | 22 blank | 0 comment | 0 complexity | 2f7a7050df456cd2d5d440e097826279 MD5 | raw file
 1<html>
 2<head>
 3<title>SWIG:Examples:tcl:simple</title>
 4</head>
 5
 6<body bgcolor="#ffffff">
 7
 8<tt>SWIG/Examples/tcl/simple/</tt>
 9<hr>
10
11<H2>Simple Tcl Example</H2>
12
13<p>
14This example illustrates how you can hook Tcl to a very simple C program containing
15a function and a global variable.
16
17<h2>The C Code</h2>
18
19Suppose you have the following C code:
20
21<blockquote>
22<pre>
23/* File : example.c */
24
25/* A global variable */
26double Foo = 3.0;
27
28/* Compute the greatest common divisor of positive integers */
29int gcd(int x, int y) {
30  int g;
31  g = y;
32  while (x &gt; 0) {
33    g = x;
34    x = y % x;
35    y = g;
36  }
37  return g;
38}
39</pre>
40</blockquote>
41
42<h2>The SWIG interface</h2>
43
44Here is a simple SWIG interface file:
45
46<blockquote>
47<pre>
48/* File: example.i */
49%module example
50
51extern int gcd(int x, int y);
52extern double Foo;
53</pre>
54</blockquote>
55
56<h2>Compilation</h2>
57
58<ol>
59<li><tt>swig -tcl <a href="example.i">example.i</a></tt>
60<p>
61<li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
62to create the extension <tt>example.so</tt>. 
63</ol>
64
65<h2>Using the extension</h2>
66
67Click <a href="runme.tcl">here</a> to see a script that calls our C functions from Tcl.
68
69<h2>Key points</h2>
70
71<ul>
72<li>Use the <tt>load</tt> statement to load your extension module into Tcl. For example:
73<blockquote>
74<pre>
75load ./example.so
76</pre>
77</blockquote>
78
79<li>C functions work just like Tcl functions. For example:
80<blockquote>
81<pre>
82set g [gcd 42 105]
83</pre>
84</blockquote>
85
86<li>C global variables are accessed as Tcl variables. For example:
87<blockquote>
88<pre>
89set a $Foo
90set Foo $newvalue
91</pre>
92</blockquote>
93</ul>
94
95<hr>
96</body>
97</html>