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