blob: d4eac12f11de7bfa01cf95187ac027b897ec1b1c [file] [log] [blame]
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11001<!--{
Andrew Gerrande07f0892012-02-14 17:02:28 +11002 "Title": "Setting up and using gccgo",
Andrew Gerrandf78d50c2012-03-05 14:31:27 +11003 "Path": "/doc/install/gccgo"
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11004}-->
Ian Lance Taylor4262ff02009-10-14 15:49:20 -07005
6<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -07007This document explains how to use gccgo, a compiler for
8the Go language. The gccgo compiler is a new frontend
9for GCC, the widely used GNU compiler. Although the
10frontend itself is under a BSD-style license, gccgo is
11normally used as part of GCC and is then covered by
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070012the <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070013License</a> (the license covers gccgo itself as part of GCC; it
14does not cover code generated by gccgo).
Ian Lance Taylor8c409002009-11-06 14:16:47 -080015</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070016
Ian Lance Taylor8c409002009-11-06 14:16:47 -080017<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070018Note that gccgo is not the <code>gc</code> compiler; see
19the <a href="/doc/install.html">Installing Go</a> instructions for that
Ian Lance Taylor8c409002009-11-06 14:16:47 -080020compiler.
21</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070022
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070023<h2 id="Releases">Releases</h2>
24
25<p>
26The simplest way to install gccgo is to install a GCC binary release
27built to include Go support. GCC binary releases are available from
28<a href="http://gcc.gnu.org/install/binaries.html">various
29websites</a> and are typically included as part of GNU/Linux
30distributions. We expect that most people who build these binaries
31will include Go support.
32</p>
33
34<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -070035The GCC 4.7.1 release and all later 4.7 releases include a complete
36<a href="/doc/go1.html">Go 1</a> compiler and libraries.
37</p>
38
39<p>
40Due to timing, the GCC 4.8.0 and 4.8.1 releases are close to but not
Ian Lance Taylor8d206d92013-11-06 13:58:35 -080041identical to Go 1.1. The GCC 4.8.2 release includes a complete Go
421.1.2 implementation.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070043</p>
44
Ian Lance Taylorb5112302014-10-22 20:41:13 -070045<p>
46The GCC 4.9 releases include a complete Go 1.2 implementation.
47</p>
48
Ian Lance Taylor8668ac02015-06-15 11:13:57 -070049<p>
50The GCC 5 releases include a complete implementation of the Go 1.4
51user libraries. The Go 1.4 runtime is not fully merged, but that
52should not be visible to Go programs.
53</p>
54
Ian Lance Taylorea736492017-01-24 09:32:29 -080055<p>
56The GCC 6 releases include a complete implementation of the Go 1.6.1
57user libraries. The Go 1.6 runtime is not fully merged, but that
58should not be visible to Go programs.
59</p>
60
61<p>
Ian Lance Taylora9345302017-09-28 09:49:46 -070062The GCC 7 releases include a complete implementation of the Go 1.8.1
63user libraries. As with earlier releases, the Go 1.8 runtime is not
64fully merged, but that should not be visible to Go programs.
65</p>
66
67<p>
68The GCC 8 releases are expected to include a complete implementation
69of the Go 1.10 release, depending on release timing. The Go 1.10
70runtime has now been fully merged into the GCC development sources,
71and concurrent garbage collection is expected to be fully supported in
72GCC 8.
Ian Lance Taylorea736492017-01-24 09:32:29 -080073</p>
74
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070075<h2 id="Source_code">Source code</h2>
76
77<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070078If you cannot use a release, or prefer to build gccgo for
79yourself,
80the gccgo source code is accessible via Subversion. The
81GCC web site
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070082has <a href="http://gcc.gnu.org/svn.html">instructions for getting the
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070083GCC source code</a>. The gccgo source code is included. As a
84convenience, a stable version of the Go support is available in
85a branch of the main GCC code
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070086repository: <code>svn://gcc.gnu.org/svn/gcc/branches/gccgo</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070087This branch is periodically updated with stable Go compiler sources.
Ian Lance Taylor8c409002009-11-06 14:16:47 -080088</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070089
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080090<p>
91Note that although <code>gcc.gnu.org</code> is the most convenient way
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070092to get the source code for the Go frontend, it is not where the master
93sources live. If you want to contribute changes to the Go frontend
Andrew Gerrandf78d50c2012-03-05 14:31:27 +110094compiler, see <a href="/doc/gccgo_contribute.html">Contributing to
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080095gccgo</a>.
96</p>
97
98
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070099<h2 id="Building">Building</h2>
100
101<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700102Building gccgo is just like building GCC
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -0800103with one or two additional options. See
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700104the <a href="http://gcc.gnu.org/install/">instructions on the gcc web
105site</a>. When you run <code>configure</code>, add the
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -0800106option <code>--enable-languages=c,c++,go</code> (along with other
107languages you may want to build). If you are targeting a 32-bit x86,
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700108then you will want to build gccgo to default to
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -0800109supporting locked compare and exchange instructions; do this by also
110using the <code>configure</code> option <code>--with-arch=i586</code>
111(or a newer architecture, depending on where you need your programs to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700112run). If you are targeting a 64-bit x86, but sometimes want to use
113the <code>-m32</code> option, then use the <code>configure</code>
114option <code>--with-arch-32=i586</code>.
115</p>
116
117<h3 id="Gold">Gold</h3>
118
119<p>
120On x86 GNU/Linux systems the gccgo compiler is able to
121use a small discontiguous stack for goroutines. This permits programs
122to run many more goroutines, since each goroutine can use a relatively
123small stack. Doing this requires using the gold linker version 2.22
124or later. You can either install GNU binutils 2.22 or later, or you
125can build gold yourself.
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800126</p>
127
128<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700129To build gold yourself, build the GNU binutils,
130using <code>--enable-gold=default</code> when you run
131the <code>configure</code> script. Before building, you must install
132the flex and bison packages. A typical sequence would look like
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800133this (you can replace <code>/opt/gold</code> with any directory to
134which you have write access):
135</p>
136
137<pre>
138cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
139[password is "anoncvs"]
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700140[The next command will create a directory named src, not binutils]
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800141cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
142mkdir binutils-objdir
143cd binutils-objdir
Ian Lance Taylor5d1d0402011-09-19 11:54:07 -0700144../src/configure --enable-gold=default --prefix=/opt/gold
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800145make
146make install
147</pre>
148
149<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700150However you install gold, when you configure gccgo, use the
151option <code>--with-ld=<var>GOLD_BINARY</var></code>.
152</p>
153
154<h3 id="Prerequisites">Prerequisites</h3>
155
156<p>
157A number of prerequisites are required to build GCC, as
158described on
159the <a href="http://gcc.gnu.org/install/prerequisites.html">gcc web
160site</a>. It is important to install all the prerequisites before
161running the gcc <code>configure</code> script.
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700162The prerequisite libraries can be conveniently downloaded using the
163script <code>contrib/download_prerequisites</code> in the GCC sources.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700164
165<h3 id="Build_commands">Build commands</h3>
166
167<p>
168Once all the prerequisites are installed, then a typical build and
169install sequence would look like this (only use
170the <code>--with-ld</code> option if you are using the gold linker as
171described above):
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800172</p>
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800173
174<pre>
175svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo
176mkdir objdir
177cd objdir
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700178../gccgo/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --with-ld=/opt/gold/bin/ld
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800179make
180make install
181</pre>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700182
183<h2 id="Using_gccgo">Using gccgo</h2>
184
185<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700186The gccgo compiler works like other gcc frontends. As of GCC 5 the gccgo
187installation also includes a version of the <code>go</code> command,
188which may be used to build Go programs as described at
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600189<a href="https://golang.org/cmd/go">https://golang.org/cmd/go</a>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700190</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700191
192<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700193To compile a file without using the <code>go</code> command:
194</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700195
196<pre>
197gccgo -c file.go
198</pre>
199
200<p>
201That produces <code>file.o</code>. To link files together to form an
202executable:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700203</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700204
205<pre>
206gccgo -o file file.o
207</pre>
208
209<p>
210To run the resulting file, you will need to tell the program where to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700211find the compiled Go packages. There are a few ways to do this:
212</p>
213
214<ul>
215<li>
216<p>
217Set the <code>LD_LIBRARY_PATH</code> environment variable:
218</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700219
220<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700221LD_LIBRARY_PATH=${prefix}/lib/gcc/MACHINE/VERSION
222[or]
223LD_LIBRARY_PATH=${prefix}/lib64/gcc/MACHINE/VERSION
224export LD_LIBRARY_PATH
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700225</pre>
226
227<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700228Here <code>${prefix}</code> is the <code>--prefix</code> option used
229when building gccgo. For a binary install this is
230normally <code>/usr</code>. Whether to use <code>lib</code>
231or <code>lib64</code> depends on the target.
232Typically <code>lib64</code> is correct for x86_64 systems,
233and <code>lib</code> is correct for other systems. The idea is to
234name the directory where <code>libgo.so</code> is found.
235</p>
236
237</li>
238
239<li>
240<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700241Passing a <code>-Wl,-R</code> option when you link (replace lib with
242lib64 if appropriate for your system):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700243</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700244
245<pre>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700246go build -gccgoflags -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700247[or]
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700248gccgo -o file file.o -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700249</pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700250</li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700251
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700252<li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700253<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700254Use the <code>-static-libgo</code> option to link statically against
255the compiled packages.
256</p>
257</li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700258
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700259<li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700260<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700261Use the <code>-static</code> option to do a fully static link (the
262default for the <code>gc</code> compiler).
263</p>
264</li>
265</ul>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700266
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800267<h2 id="Options">Options</h2>
268
269<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700270The gccgo compiler supports all GCC options
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800271that are language independent, notably the <code>-O</code>
272and <code>-g</code> options.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700273</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800274
275<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700276The <code>-fgo-pkgpath=PKGPATH</code> option may be used to set a
277unique prefix for the package being compiled.
278This option is automatically used by the go command, but you may want
279to use it if you invoke gccgo directly.
280This option is intended for use with large
281programs that contain many packages, in order to allow multiple
282packages to use the same identifier as the package name.
283The <code>PKGPATH</code> may be any string; a good choice for the
284string is the path used to import the package.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700285</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800286
287<p>
288The <code>-I</code> and <code>-L</code> options, which are synonyms
289for the compiler, may be used to set the search path for finding
290imports.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700291These options are not needed if you build with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700292</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800293
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700294<h2 id="Imports">Imports</h2>
295
296<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700297When you compile a file that exports something, the export
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700298information will be stored directly in the object file.
299If you build with gccgo directly, rather than with the go command,
300then when you import a package, you must tell gccgo how to find the
301file.
302</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700303
304<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700305When you import the package <var>FILE</var> with gccgo,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700306it will look for the import data in the following files, and use the
307first one that it finds.
308
309<ul>
310<li><code><var>FILE</var>.gox</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700311<li><code>lib<var>FILE</var>.so</code>
312<li><code>lib<var>FILE</var>.a</code>
Peter Collingbournea5cc5ba2013-12-06 13:33:10 -0800313<li><code><var>FILE</var>.o</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700314</ul>
315
316<p>
317<code><var>FILE</var>.gox</code>, when used, will typically contain
318nothing but export data. This can be generated from
319<code><var>FILE</var>.o</code> via
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700320</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700321
322<pre>
323objcopy -j .go_export FILE.o FILE.gox
324</pre>
325
326<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700327The gccgo compiler will look in the current
328directory for import files. In more complex scenarios you
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700329may pass the <code>-I</code> or <code>-L</code> option to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700330gccgo. Both options take directories to search. The
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700331<code>-L</code> option is also passed to the linker.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700332</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700333
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700334<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700335The gccgo compiler does not currently (2015-06-15) record
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700336the file name of imported packages in the object file. You must
337arrange for the imported data to be linked into the program.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700338Again, this is not necessary when building with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700339</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700340
341<pre>
342gccgo -c mypackage.go # Exports mypackage
343gccgo -c main.go # Imports mypackage
344gccgo -o main main.o mypackage.o # Explicitly links with mypackage.o
345</pre>
346
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700347<h2 id="Debugging">Debugging</h2>
348
349<p>
350If you use the <code>-g</code> option when you compile, you can run
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700351<code>gdb</code> on your executable. The debugger has only limited
352knowledge about Go. You can set breakpoints, single-step,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700353etc. You can print variables, but they will be printed as though they
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700354had C/C++ types. For numeric types this doesn't matter. Go strings
355and interfaces will show up as two-element structures. Go
356maps and channels are always represented as C pointers to run-time
357structures.
358</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700359
360<h2 id="C_Interoperability">C Interoperability</h2>
361
362<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700363When using gccgo there is limited interoperability with C,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700364or with C++ code compiled using <code>extern "C"</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700365</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700366
367<h3 id="Types">Types</h3>
368
369<p>
Ian Lance Taylorea736492017-01-24 09:32:29 -0800370Basic types map directly: an <code>int32</code> in Go is
371an <code>int32_t</code> in C, an <code>int64</code> is
372an <code>int64_t</code>, etc.
373The Go type <code>int</code> is an integer that is the same size as a
374pointer, and as such corresponds to the C type <code>intptr_t</code>.
375Go <code>byte</code> is equivalent to C <code>unsigned char</code>.
376Pointers in Go are pointers in C.
377A Go <code>struct</code> is the same as C <code>struct</code> with the
378same fields and types.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700379</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700380
381<p>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700382The Go <code>string</code> type is currently defined as a two-element
383structure (this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700384</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700385
386<pre>
387struct __go_string {
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700388 const unsigned char *__data;
Ian Lance Taylorea736492017-01-24 09:32:29 -0800389 intptr_t __length;
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700390};
391</pre>
392
393<p>
394You can't pass arrays between C and Go. However, a pointer to an
395array in Go is equivalent to a C pointer to the
396equivalent of the element type.
397For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>,
398assuming that the C pointer does point to 10 elements.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700399</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700400
401<p>
402A slice in Go is a structure. The current definition is
403(this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700404</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700405
406<pre>
407struct __go_slice {
408 void *__values;
Ian Lance Taylorea736492017-01-24 09:32:29 -0800409 intptr_t __count;
410 intptr_t __capacity;
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700411};
412</pre>
413
414<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700415The type of a Go function is a pointer to a struct (this is
416<b style="color: red;">subject to change</b>). The first field in the
417struct points to the code of the function, which will be equivalent to
418a pointer to a C function whose parameter types are equivalent, with
419an additional trailing parameter. The trailing parameter is the
420closure, and the argument to pass is a pointer to the Go function
421struct.
422
423When a Go function returns more than one value, the C function returns
424a struct. For example, these functions are roughly equivalent:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700425</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700426
427<pre>
Rob Pike80e25fc2011-01-19 23:07:38 -0500428func GoFunction(int) (int, float64)
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700429struct { int i; float64 f; } CFunction(int, void*)
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700430</pre>
431
432<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700433Go <code>interface</code>, <code>channel</code>, and <code>map</code>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700434types have no corresponding C type (<code>interface</code> is a
435two-element struct and <code>channel</code> and <code>map</code> are
436pointers to structs in C, but the structs are deliberately undocumented). C
437<code>enum</code> types correspond to some integer type, but precisely
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700438which one is difficult to predict in general; use a cast. C <code>union</code>
439types have no corresponding Go type. C <code>struct</code> types containing
440bitfields have no corresponding Go type. C++ <code>class</code> types have
441no corresponding Go type.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700442</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700443
444<p>
445Memory allocation is completely different between C and Go, as Go uses
446garbage collection. The exact guidelines in this area are undetermined,
447but it is likely that it will be permitted to pass a pointer to allocated
448memory from C to Go. The responsibility of eventually freeing the pointer
449will remain with C side, and of course if the C side frees the pointer
450while the Go side still has a copy the program will fail. When passing a
451pointer from Go to C, the Go function must retain a visible copy of it in
452some Go variable. Otherwise the Go garbage collector may delete the
453pointer while the C function is still using it.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700454</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700455
456<h3 id="Function_names">Function names</h3>
457
458<p>
459Go code can call C functions directly using a Go extension implemented
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700460in gccgo: a function declaration may be preceded by
461<code>//extern NAME</code>. For example, here is how the C function
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700462<code>open</code> can be declared in Go:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700463</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700464
465<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700466//extern open
467func c_open(name *byte, mode int, perm int) int
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700468</pre>
469
470<p>
Jeremy Jackins7e054262012-03-19 08:26:36 +1100471The C function naturally expects a NUL-terminated string, which in
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700472Go is equivalent to a pointer to an array (not a slice!) of
473<code>byte</code> with a terminating zero byte. So a sample call
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700474from Go would look like (after importing the <code>syscall</code> package):
475</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700476
477<pre>
478var name = [4]byte{'f', 'o', 'o', 0};
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700479i := c_open(&amp;name[0], syscall.O_RDONLY, 0);
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700480</pre>
481
482<p>
483(this serves as an example only, to open a file in Go please use Go's
484<code>os.Open</code> function instead).
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700485</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700486
487<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700488Note that if the C function can block, such as in a call
489to <code>read</code>, calling the C function may block the Go program.
490Unless you have a clear understanding of what you are doing, all calls
491between C and Go should be implemented through cgo or SWIG, as for
492the <code>gc</code> compiler.
493</p>
494
495<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700496The name of Go functions accessed from C is subject to change. At present
497the name of a Go function that does not have a receiver is
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700498<code>prefix.package.Functionname</code>. The prefix is set by
499the <code>-fgo-prefix</code> option used when the package is compiled;
Russ Coxae7497b2012-02-06 13:34:35 -0500500if the option is not used, the default is <code>go</code>.
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700501To call the function from C you must set the name using
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700502a GCC extension.
503</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700504
505<pre>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700506extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700507</pre>
508
509<h3 id="Automatic_generation_of_Go_declarations_from_C_source_code">
510Automatic generation of Go declarations from C source code</h3>
511
512<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700513The Go version of GCC supports automatically generating
514Go declarations from C code. The facility is rather awkward, and most
515users should use the <a href="/cmd/cgo">cgo</a> program with
516the <code>-gccgo</code> option instead.
517</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700518
519<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700520Compile your C code as usual, and add the option
521<code>-fdump-go-spec=<var>FILENAME</var></code>. This will create the
522file <code><var>FILENAME</var></code> as a side effect of the
523compilation. This file will contain Go declarations for the types,
524variables and functions declared in the C code. C types that can not
525be represented in Go will be recorded as comments in the Go code. The
526generated file will not have a <code>package</code> declaration, but
527can otherwise be compiled directly by gccgo.
528</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700529
530<p>
531This procedure is full of unstated caveats and restrictions and we make no
532guarantee that it will not change in the future. It is more useful as a
533starting point for real Go code than as a regular procedure.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700534</p>