PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/go/simple/index.html

#
HTML | 128 lines | 103 code | 25 blank | 0 comment | 0 complexity | f0e3f0eabfc20015fa7c0eeedb80a874 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:go:simple</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/go/simple/</tt>
  7. <hr>
  8. <H2>Simple Go Example</H2>
  9. <p>
  10. This example illustrates how you can hook Go 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. These are the instructions if you are using <tt>6g</tt>/<tt>8g</tt>
  44. rather than <tt>gccgo</tt>.
  45. <ol>
  46. <li>Run <tt>swig -go <a href="example.i">example.i</a></tt>. This
  47. will create the three
  48. files <tt>example.go</tt>, <tt>example_gc.c</tt>,
  49. and <tt>example_wrap.c</tt>.
  50. <li>Compile <tt><a href="example.go">example.go</a></tt>
  51. using <tt>6g</tt> or <tt>8g</tt>; e.g., <tt>6g example.go</tt>.
  52. <li>Compile <tt><a href="example_gc.c">example_gc.c</a></tt>
  53. using <tt>6c</tt> or <tt>8c</tt>; e.g., <tt>6c example_gc.c</tt>.
  54. <li>Put the two object files together into an archive
  55. named <tt>example.a</tt>; e.g., <tt>gopack grc example.a example.6
  56. example_gc.6</tt>.
  57. <li>Compile the <tt><a href="example_wrap.c">example_wrap.c</a></tt>
  58. file using your standard C compiler with the <tt>-fpic</tt> option;
  59. e.g., <tt>gcc -c -O -fpic example_wrap.c</tt>.
  60. <li>Also compile the actual code, not generated by SWIG; e.g., <tt>gcc
  61. -c -O -fpic example.c</tt>.
  62. <li>Put the gcc compiled object files into a shared library;
  63. e.g., <tt>gcc -shared -o example.so example_wrap.o example.o</tt>.
  64. <li>Compile the program which demonstrates how to use the library;
  65. e.g., <tt>6g runme.go</tt>.
  66. <li>Link the program; e.g., <tt>6l -o runme runme.6</tt>.
  67. <li>Now you should have a program <tt>runme</tt>.
  68. </ol>
  69. <h2>Using the extension</h2>
  70. The Go program which demonstrates calling the C functions from Go
  71. is <a href="runme.go">runme.go</a>.
  72. <h2>Key points</h2>
  73. <ul>
  74. <li>Use the <tt>import</tt> statement to load your extension module from Go. For example:
  75. <blockquote>
  76. <pre>
  77. import "example"
  78. </pre>
  79. </blockquote>
  80. <li>C functions work just like Go functions. However, the function
  81. names are automatically capitalized in order to make the names
  82. visible from other Go packages. For example:
  83. <blockquote>
  84. <pre>
  85. g := example.Gcd(42,105)
  86. </pre>
  87. </blockquote>
  88. (If there are name conflicts, you can use the <tt>%rename</tt>
  89. directive in the .i file or the <tt>-rename</tt> option to Go to
  90. rename one or the other symbol).
  91. <li>C global variables are accessed using getter and setter
  92. functions. The getter function is named <tt>Get</tt> followed by
  93. the capitalized name of the C variable. The Setter function
  94. uses <tt>Set</tt> instead of <tt>Get</tt>.
  95. <blockquote>
  96. <pre>
  97. a = example.GetFoo()
  98. </pre>
  99. </blockquote>
  100. </ul>
  101. <hr>
  102. </body>
  103. </html>