File: index.html

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (128 lines) | stat: -rw-r--r-- 3,194 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<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 &gt; 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>