blob: ef27fd1818265128e808900067fe71b47072dea1 [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 Taylor4262ff02009-10-14 15:49:20 -070055<h2 id="Source_code">Source code</h2>
56
57<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070058If you cannot use a release, or prefer to build gccgo for
59yourself,
60the gccgo source code is accessible via Subversion. The
61GCC web site
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070062has <a href="http://gcc.gnu.org/svn.html">instructions for getting the
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070063GCC source code</a>. The gccgo source code is included. As a
64convenience, a stable version of the Go support is available in
65a branch of the main GCC code
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070066repository: <code>svn://gcc.gnu.org/svn/gcc/branches/gccgo</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070067This branch is periodically updated with stable Go compiler sources.
Ian Lance Taylor8c409002009-11-06 14:16:47 -080068</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070069
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080070<p>
71Note that although <code>gcc.gnu.org</code> is the most convenient way
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070072to get the source code for the Go frontend, it is not where the master
73sources live. If you want to contribute changes to the Go frontend
Andrew Gerrandf78d50c2012-03-05 14:31:27 +110074compiler, see <a href="/doc/gccgo_contribute.html">Contributing to
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080075gccgo</a>.
76</p>
77
78
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070079<h2 id="Building">Building</h2>
80
81<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070082Building gccgo is just like building GCC
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -080083with one or two additional options. See
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070084the <a href="http://gcc.gnu.org/install/">instructions on the gcc web
85site</a>. When you run <code>configure</code>, add the
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -080086option <code>--enable-languages=c,c++,go</code> (along with other
87languages you may want to build). If you are targeting a 32-bit x86,
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070088then you will want to build gccgo to default to
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -080089supporting locked compare and exchange instructions; do this by also
90using the <code>configure</code> option <code>--with-arch=i586</code>
91(or a newer architecture, depending on where you need your programs to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070092run). If you are targeting a 64-bit x86, but sometimes want to use
93the <code>-m32</code> option, then use the <code>configure</code>
94option <code>--with-arch-32=i586</code>.
95</p>
96
97<h3 id="Gold">Gold</h3>
98
99<p>
100On x86 GNU/Linux systems the gccgo compiler is able to
101use a small discontiguous stack for goroutines. This permits programs
102to run many more goroutines, since each goroutine can use a relatively
103small stack. Doing this requires using the gold linker version 2.22
104or later. You can either install GNU binutils 2.22 or later, or you
105can build gold yourself.
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800106</p>
107
108<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700109To build gold yourself, build the GNU binutils,
110using <code>--enable-gold=default</code> when you run
111the <code>configure</code> script. Before building, you must install
112the flex and bison packages. A typical sequence would look like
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800113this (you can replace <code>/opt/gold</code> with any directory to
114which you have write access):
115</p>
116
117<pre>
118cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
119[password is "anoncvs"]
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700120[The next command will create a directory named src, not binutils]
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800121cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
122mkdir binutils-objdir
123cd binutils-objdir
Ian Lance Taylor5d1d0402011-09-19 11:54:07 -0700124../src/configure --enable-gold=default --prefix=/opt/gold
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800125make
126make install
127</pre>
128
129<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700130However you install gold, when you configure gccgo, use the
131option <code>--with-ld=<var>GOLD_BINARY</var></code>.
132</p>
133
134<h3 id="Prerequisites">Prerequisites</h3>
135
136<p>
137A number of prerequisites are required to build GCC, as
138described on
139the <a href="http://gcc.gnu.org/install/prerequisites.html">gcc web
140site</a>. It is important to install all the prerequisites before
141running the gcc <code>configure</code> script.
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700142The prerequisite libraries can be conveniently downloaded using the
143script <code>contrib/download_prerequisites</code> in the GCC sources.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700144
145<h3 id="Build_commands">Build commands</h3>
146
147<p>
148Once all the prerequisites are installed, then a typical build and
149install sequence would look like this (only use
150the <code>--with-ld</code> option if you are using the gold linker as
151described above):
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800152</p>
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800153
154<pre>
155svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo
156mkdir objdir
157cd objdir
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700158../gccgo/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --with-ld=/opt/gold/bin/ld
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800159make
160make install
161</pre>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700162
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700163<h3 id="Ubuntu">A note on Ubuntu</h3>
164
165<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700166Current versions of Ubuntu and versions of GCC before 4.8 disagree on
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700167where system libraries and header files are found. This is not a
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700168gccgo issue. When building older versions of GCC, setting these
169environment variables while configuring and building gccgo may fix the
170problem.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700171</p>
172
173<pre>
174LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
175C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
176CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
177export LIBRARY_PATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH
178</pre>
179
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700180<h2 id="Using_gccgo">Using gccgo</h2>
181
182<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700183The gccgo compiler works like other gcc frontends. As of GCC 5 the gccgo
184installation also includes a version of the <code>go</code> command,
185which may be used to build Go programs as described at
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600186<a href="https://golang.org/cmd/go">https://golang.org/cmd/go</a>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700187</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700188
189<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700190To compile a file without using the <code>go</code> command:
191</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700192
193<pre>
194gccgo -c file.go
195</pre>
196
197<p>
198That produces <code>file.o</code>. To link files together to form an
199executable:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700200</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700201
202<pre>
203gccgo -o file file.o
204</pre>
205
206<p>
207To run the resulting file, you will need to tell the program where to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700208find the compiled Go packages. There are a few ways to do this:
209</p>
210
211<ul>
212<li>
213<p>
214Set the <code>LD_LIBRARY_PATH</code> environment variable:
215</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700216
217<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700218LD_LIBRARY_PATH=${prefix}/lib/gcc/MACHINE/VERSION
219[or]
220LD_LIBRARY_PATH=${prefix}/lib64/gcc/MACHINE/VERSION
221export LD_LIBRARY_PATH
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700222</pre>
223
224<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700225Here <code>${prefix}</code> is the <code>--prefix</code> option used
226when building gccgo. For a binary install this is
227normally <code>/usr</code>. Whether to use <code>lib</code>
228or <code>lib64</code> depends on the target.
229Typically <code>lib64</code> is correct for x86_64 systems,
230and <code>lib</code> is correct for other systems. The idea is to
231name the directory where <code>libgo.so</code> is found.
232</p>
233
234</li>
235
236<li>
237<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700238Passing a <code>-Wl,-R</code> option when you link (replace lib with
239lib64 if appropriate for your system):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700240</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700241
242<pre>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700243go build -gccgoflags -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700244[or]
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700245gccgo -o file file.o -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700246</pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700247</li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700248
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700249<li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700250<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700251Use the <code>-static-libgo</code> option to link statically against
252the compiled packages.
253</p>
254</li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700255
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700256<li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700257<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700258Use the <code>-static</code> option to do a fully static link (the
259default for the <code>gc</code> compiler).
260</p>
261</li>
262</ul>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700263
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800264<h2 id="Options">Options</h2>
265
266<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700267The gccgo compiler supports all GCC options
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800268that are language independent, notably the <code>-O</code>
269and <code>-g</code> options.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700270</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800271
272<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700273The <code>-fgo-pkgpath=PKGPATH</code> option may be used to set a
274unique prefix for the package being compiled.
275This option is automatically used by the go command, but you may want
276to use it if you invoke gccgo directly.
277This option is intended for use with large
278programs that contain many packages, in order to allow multiple
279packages to use the same identifier as the package name.
280The <code>PKGPATH</code> may be any string; a good choice for the
281string is the path used to import the package.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700282</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800283
284<p>
285The <code>-I</code> and <code>-L</code> options, which are synonyms
286for the compiler, may be used to set the search path for finding
287imports.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700288These options are not needed if you build with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700289</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800290
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700291<h2 id="Imports">Imports</h2>
292
293<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700294When you compile a file that exports something, the export
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700295information will be stored directly in the object file.
296If you build with gccgo directly, rather than with the go command,
297then when you import a package, you must tell gccgo how to find the
298file.
299</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700300
301<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700302When you import the package <var>FILE</var> with gccgo,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700303it will look for the import data in the following files, and use the
304first one that it finds.
305
306<ul>
307<li><code><var>FILE</var>.gox</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700308<li><code>lib<var>FILE</var>.so</code>
309<li><code>lib<var>FILE</var>.a</code>
Peter Collingbournea5cc5ba2013-12-06 13:33:10 -0800310<li><code><var>FILE</var>.o</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700311</ul>
312
313<p>
314<code><var>FILE</var>.gox</code>, when used, will typically contain
315nothing but export data. This can be generated from
316<code><var>FILE</var>.o</code> via
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700317</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700318
319<pre>
320objcopy -j .go_export FILE.o FILE.gox
321</pre>
322
323<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700324The gccgo compiler will look in the current
325directory for import files. In more complex scenarios you
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700326may pass the <code>-I</code> or <code>-L</code> option to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700327gccgo. Both options take directories to search. The
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700328<code>-L</code> option is also passed to the linker.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700329</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700330
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700331<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700332The gccgo compiler does not currently (2015-06-15) record
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700333the file name of imported packages in the object file. You must
334arrange for the imported data to be linked into the program.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700335Again, this is not necessary when building with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700336</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700337
338<pre>
339gccgo -c mypackage.go # Exports mypackage
340gccgo -c main.go # Imports mypackage
341gccgo -o main main.o mypackage.o # Explicitly links with mypackage.o
342</pre>
343
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700344<h2 id="Debugging">Debugging</h2>
345
346<p>
347If you use the <code>-g</code> option when you compile, you can run
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700348<code>gdb</code> on your executable. The debugger has only limited
349knowledge about Go. You can set breakpoints, single-step,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700350etc. You can print variables, but they will be printed as though they
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700351had C/C++ types. For numeric types this doesn't matter. Go strings
352and interfaces will show up as two-element structures. Go
353maps and channels are always represented as C pointers to run-time
354structures.
355</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700356
357<h2 id="C_Interoperability">C Interoperability</h2>
358
359<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700360When using gccgo there is limited interoperability with C,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700361or with C++ code compiled using <code>extern "C"</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700362</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700363
364<h3 id="Types">Types</h3>
365
366<p>
367Basic types map directly: an <code>int</code> in Go is an <code>int</code>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700368in C, an <code>int32</code> is an <code>int32_t</code>,
369etc. Go <code>byte</code> is equivalent to C <code>unsigned
370char</code>.
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700371Pointers in Go are pointers in C. A Go <code>struct</code> is the same as C
372<code>struct</code> with the same fields and types.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700373</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700374
375<p>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700376The Go <code>string</code> type is currently defined as a two-element
377structure (this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700378</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700379
380<pre>
381struct __go_string {
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700382 const unsigned char *__data;
383 int __length;
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700384};
385</pre>
386
387<p>
388You can't pass arrays between C and Go. However, a pointer to an
389array in Go is equivalent to a C pointer to the
390equivalent of the element type.
391For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>,
392assuming that the C pointer does point to 10 elements.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700393</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700394
395<p>
396A slice in Go is a structure. The current definition is
397(this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700398</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700399
400<pre>
401struct __go_slice {
402 void *__values;
403 int __count;
404 int __capacity;
405};
406</pre>
407
408<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700409The type of a Go function is a pointer to a struct (this is
410<b style="color: red;">subject to change</b>). The first field in the
411struct points to the code of the function, which will be equivalent to
412a pointer to a C function whose parameter types are equivalent, with
413an additional trailing parameter. The trailing parameter is the
414closure, and the argument to pass is a pointer to the Go function
415struct.
416
417When a Go function returns more than one value, the C function returns
418a struct. For example, these functions are roughly equivalent:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700419</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700420
421<pre>
Rob Pike80e25fc2011-01-19 23:07:38 -0500422func GoFunction(int) (int, float64)
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700423struct { int i; float64 f; } CFunction(int, void*)
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700424</pre>
425
426<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700427Go <code>interface</code>, <code>channel</code>, and <code>map</code>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700428types have no corresponding C type (<code>interface</code> is a
429two-element struct and <code>channel</code> and <code>map</code> are
430pointers to structs in C, but the structs are deliberately undocumented). C
431<code>enum</code> types correspond to some integer type, but precisely
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700432which one is difficult to predict in general; use a cast. C <code>union</code>
433types have no corresponding Go type. C <code>struct</code> types containing
434bitfields have no corresponding Go type. C++ <code>class</code> types have
435no corresponding Go type.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700436</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700437
438<p>
439Memory allocation is completely different between C and Go, as Go uses
440garbage collection. The exact guidelines in this area are undetermined,
441but it is likely that it will be permitted to pass a pointer to allocated
442memory from C to Go. The responsibility of eventually freeing the pointer
443will remain with C side, and of course if the C side frees the pointer
444while the Go side still has a copy the program will fail. When passing a
445pointer from Go to C, the Go function must retain a visible copy of it in
446some Go variable. Otherwise the Go garbage collector may delete the
447pointer while the C function is still using it.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700448</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700449
450<h3 id="Function_names">Function names</h3>
451
452<p>
453Go code can call C functions directly using a Go extension implemented
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700454in gccgo: a function declaration may be preceded by
455<code>//extern NAME</code>. For example, here is how the C function
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700456<code>open</code> can be declared in Go:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700457</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700458
459<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700460//extern open
461func c_open(name *byte, mode int, perm int) int
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700462</pre>
463
464<p>
Jeremy Jackins7e054262012-03-19 08:26:36 +1100465The C function naturally expects a NUL-terminated string, which in
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700466Go is equivalent to a pointer to an array (not a slice!) of
467<code>byte</code> with a terminating zero byte. So a sample call
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700468from Go would look like (after importing the <code>syscall</code> package):
469</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700470
471<pre>
472var name = [4]byte{'f', 'o', 'o', 0};
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700473i := c_open(&amp;name[0], syscall.O_RDONLY, 0);
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700474</pre>
475
476<p>
477(this serves as an example only, to open a file in Go please use Go's
478<code>os.Open</code> function instead).
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700479</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700480
481<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700482Note that if the C function can block, such as in a call
483to <code>read</code>, calling the C function may block the Go program.
484Unless you have a clear understanding of what you are doing, all calls
485between C and Go should be implemented through cgo or SWIG, as for
486the <code>gc</code> compiler.
487</p>
488
489<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700490The name of Go functions accessed from C is subject to change. At present
491the name of a Go function that does not have a receiver is
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700492<code>prefix.package.Functionname</code>. The prefix is set by
493the <code>-fgo-prefix</code> option used when the package is compiled;
Russ Coxae7497b2012-02-06 13:34:35 -0500494if the option is not used, the default is <code>go</code>.
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700495To call the function from C you must set the name using
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700496a GCC extension.
497</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700498
499<pre>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700500extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700501</pre>
502
503<h3 id="Automatic_generation_of_Go_declarations_from_C_source_code">
504Automatic generation of Go declarations from C source code</h3>
505
506<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700507The Go version of GCC supports automatically generating
508Go declarations from C code. The facility is rather awkward, and most
509users should use the <a href="/cmd/cgo">cgo</a> program with
510the <code>-gccgo</code> option instead.
511</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700512
513<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700514Compile your C code as usual, and add the option
515<code>-fdump-go-spec=<var>FILENAME</var></code>. This will create the
516file <code><var>FILENAME</var></code> as a side effect of the
517compilation. This file will contain Go declarations for the types,
518variables and functions declared in the C code. C types that can not
519be represented in Go will be recorded as comments in the Go code. The
520generated file will not have a <code>package</code> declaration, but
521can otherwise be compiled directly by gccgo.
522</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700523
524<p>
525This procedure is full of unstated caveats and restrictions and we make no
526guarantee that it will not change in the future. It is more useful as a
527starting point for real Go code than as a regular procedure.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700528</p>
Vinu Rajashekhar658d5212010-07-20 00:06:20 -0700529
Scott Lawrence76dccbc2010-08-30 09:58:27 +1000530<h2 id="RTEMS_Port">RTEMS Port</h2>
Vinu Rajashekhar658d5212010-07-20 00:06:20 -0700531<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700532The gccgo compiler has been ported to <a href="http://www.rtems.com/">
Vinu Rajashekhar658d5212010-07-20 00:06:20 -0700533<code>RTEMS</code></a>. <code>RTEMS</code> is a real-time executive
534that provides a high performance environment for embedded applications
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700535on a range of processors and embedded hardware. The current gccgo
Vinu Rajashekhar658d5212010-07-20 00:06:20 -0700536port is for x86. The goal is to extend the port to most of the
537<a href="http://www.rtems.org/wiki/index.php/SupportedCPUs">
538architectures supported by <code>RTEMS</code></a>. For more information on the port,
539as well as instructions on how to install it, please see this
Robert Henckea9288d62014-04-28 06:27:22 -0700540<a href="http://www.rtems.org/wiki/index.php/GCCGoRTEMS"><code>RTEMS</code> Wiki page</a>.