PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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