PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms 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
  1<html>
  2<head>
  3<title>SWIG:Examples:go:simple</title>
  4</head>
  5
  6<body bgcolor="#ffffff">
  7
  8
  9<tt>SWIG/Examples/go/simple/</tt>
 10<hr>
 11
 12<H2>Simple Go Example</H2>
 13
 14<p>
 15This example illustrates how you can hook Go to a very simple C program containing
 16a function and a global variable.
 17
 18<h2>The C Code</h2>
 19
 20Suppose you have the following C code:
 21
 22<blockquote>
 23<pre>
 24/* File : example.c */
 25
 26/* A global variable */
 27double Foo = 3.0;
 28
 29/* Compute the greatest common divisor of positive integers */
 30int gcd(int x, int y) {
 31  int g;
 32  g = y;
 33  while (x &gt; 0) {
 34    g = x;
 35    x = y % x;
 36    y = g;
 37  }
 38  return g;
 39}
 40</pre>
 41</blockquote>
 42
 43<h2>The SWIG interface</h2>
 44
 45Here is a simple SWIG interface file:
 46
 47<blockquote>
 48<pre>
 49/* File: example.i */
 50%module example
 51
 52extern int gcd(int x, int y);
 53extern double Foo;
 54</pre>
 55</blockquote>
 56
 57<h2>Compilation</h2>
 58
 59These are the instructions if you are using <tt>6g</tt>/<tt>8g</tt>
 60rather than <tt>gccgo</tt>.
 61
 62<ol>
 63<li>Run <tt>swig -go <a href="example.i">example.i</a></tt>.  This
 64  will create the three
 65  files <tt>example.go</tt>, <tt>example_gc.c</tt>,
 66  and <tt>example_wrap.c</tt>.
 67<li>Compile <tt><a href="example.go">example.go</a></tt>
 68  using <tt>6g</tt> or <tt>8g</tt>; e.g., <tt>6g example.go</tt>.
 69<li>Compile <tt><a href="example_gc.c">example_gc.c</a></tt>
 70  using <tt>6c</tt> or <tt>8c</tt>; e.g., <tt>6c example_gc.c</tt>.
 71<li>Put the two object files together into an archive
 72  named <tt>example.a</tt>; e.g., <tt>gopack grc example.a example.6
 73  example_gc.6</tt>.
 74<li>Compile the <tt><a href="example_wrap.c">example_wrap.c</a></tt>
 75  file using your standard C compiler with the <tt>-fpic</tt> option;
 76  e.g., <tt>gcc -c -O -fpic example_wrap.c</tt>.
 77<li>Also compile the actual code, not generated by SWIG; e.g., <tt>gcc
 78    -c -O -fpic example.c</tt>.
 79<li>Put the gcc compiled object files into a shared library;
 80  e.g., <tt>gcc -shared -o example.so example_wrap.o example.o</tt>.
 81<li>Compile the program which demonstrates how to use the library;
 82  e.g., <tt>6g runme.go</tt>.
 83<li>Link the program; e.g., <tt>6l -o runme runme.6</tt>.
 84<li>Now you should have a program <tt>runme</tt>.
 85</ol>
 86
 87<h2>Using the extension</h2>
 88
 89The Go program which demonstrates calling the C functions from Go
 90is <a href="runme.go">runme.go</a>.
 91
 92<h2>Key points</h2>
 93
 94<ul>
 95<li>Use the <tt>import</tt> statement to load your extension module from Go. For example:
 96<blockquote>
 97<pre>
 98import "example"
 99</pre>
100</blockquote>
101
102<li>C functions work just like Go functions.  However, the function
103  names are automatically capitalized in order to make the names
104  visible from other Go packages.  For example:
105<blockquote>
106<pre>
107g := example.Gcd(42,105)
108</pre>
109</blockquote>
110
111(If there are name conflicts, you can use the <tt>%rename</tt>
112directive in the .i file or the <tt>-rename</tt> option to Go to
113rename one or the other symbol).
114
115<li>C global variables are accessed using getter and setter
116  functions.  The getter function is named <tt>Get</tt> followed by
117  the capitalized name of the C variable.  The Setter function
118  uses <tt>Set</tt> instead of <tt>Get</tt>.
119<blockquote>
120<pre>
121a = example.GetFoo()
122</pre>
123</blockquote>
124</ul>
125
126<hr>
127</body>
128</html>