PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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