| <html> | |
| <head> | |
| <title>SWIG:Examples:go:simple</title> | |
| </head> | |
| <body bgcolor="#ffffff"> | |
| <tt>SWIG/Examples/go/simple/</tt> | |
| <hr> | |
| <H2>Simple Go Example</H2> | |
| <p> | |
| This example illustrates how you can hook Go to a very simple C program containing | |
| a function and a global variable. | |
| <h2>The C Code</h2> | |
| Suppose you have the following C code: | |
| <blockquote> | |
| <pre> | |
| /* File : example.c */ | |
| /* A global variable */ | |
| double Foo = 3.0; | |
| /* Compute the greatest common divisor of positive integers */ | |
| int gcd(int x, int y) { | |
| int g; | |
| g = y; | |
| while (x > 0) { | |
| g = x; | |
| x = y % x; | |
| y = g; | |
| } | |
| return g; | |
| } | |
| </pre> | |
| </blockquote> | |
| <h2>The SWIG interface</h2> | |
| Here is a simple SWIG interface file: | |
| <blockquote> | |
| <pre> | |
| /* File: example.i */ | |
| %module example | |
| extern int gcd(int x, int y); | |
| extern double Foo; | |
| </pre> | |
| </blockquote> | |
| <h2>Compilation</h2> | |
| These are the instructions if you are using <tt>6g</tt>/<tt>8g</tt> | |
| rather than <tt>gccgo</tt>. | |
| <ol> | |
| <li>Run <tt>swig -go <a href="example.i">example.i</a></tt>. This | |
| will create the three | |
| files <tt>example.go</tt>, <tt>example_gc.c</tt>, | |
| and <tt>example_wrap.c</tt>. | |
| <li>Compile <tt><a href="example.go">example.go</a></tt> | |
| using <tt>6g</tt> or <tt>8g</tt>; e.g., <tt>6g example.go</tt>. | |
| <li>Compile <tt><a href="example_gc.c">example_gc.c</a></tt> | |
| using <tt>6c</tt> or <tt>8c</tt>; e.g., <tt>6c example_gc.c</tt>. | |
| <li>Put the two object files together into an archive | |
| named <tt>example.a</tt>; e.g., <tt>gopack grc example.a example.6 | |
| example_gc.6</tt>. | |
| <li>Compile the <tt><a href="example_wrap.c">example_wrap.c</a></tt> | |
| file using your standard C compiler with the <tt>-fPIC</tt> option; | |
| e.g., <tt>gcc -c -O -fPIC example_wrap.c</tt>. | |
| <li>Also compile the actual code, not generated by SWIG; e.g., <tt>gcc | |
| -c -O -fPIC example.c</tt>. | |
| <li>Put the gcc compiled object files into a shared library; | |
| e.g., <tt>gcc -shared -o example.so example_wrap.o example.o</tt>. | |
| <li>Compile the program which demonstrates how to use the library; | |
| e.g., <tt>6g runme.go</tt>. | |
| <li>Link the program; e.g., <tt>6l -o runme runme.6</tt>. | |
| <li>Now you should have a program <tt>runme</tt>. | |
| </ol> | |
| <h2>Using the extension</h2> | |
| The Go program which demonstrates calling the C functions from Go | |
| is <a href="runme.go">runme.go</a>. | |
| <h2>Key points</h2> | |
| <ul> | |
| <li>Use the <tt>import</tt> statement to load your extension module from Go. For example: | |
| <blockquote> | |
| <pre> | |
| import "example" | |
| </pre> | |
| </blockquote> | |
| <li>C functions work just like Go functions. However, the function | |
| names are automatically capitalized in order to make the names | |
| visible from other Go packages. For example: | |
| <blockquote> | |
| <pre> | |
| g := example.Gcd(42,105) | |
| </pre> | |
| </blockquote> | |
| (If there are name conflicts, you can use the <tt>%rename</tt> | |
| directive in the .i file or the <tt>-rename</tt> option to Go to | |
| rename one or the other symbol). | |
| <li>C global variables are accessed using getter and setter | |
| functions. The getter function is named <tt>Get</tt> followed by | |
| the capitalized name of the C variable. The Setter function | |
| uses <tt>Set</tt> instead of <tt>Get</tt>. | |
| <blockquote> | |
| <pre> | |
| a = example.GetFoo() | |
| </pre> | |
| </blockquote> | |
| </ul> | |
| <hr> | |
| </body> | |
| </html> |