blob: 08415a871bfa764add5a60c2d5d26f7b1db0e62e [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
Alberto Donizetti77aea692019-09-07 16:31:36 +02008the Go language. The gccgo compiler is a new frontend
9for GCC, the widely used GNU compiler. Although the
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070010frontend itself is under a BSD-style license, gccgo is
11normally used as part of GCC and is then covered by
Suriyaa Sundararuban1041ac82018-06-13 07:06:04 +000012the <a href="https://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
Alberto Donizetti77aea692019-09-07 16:31:36 +020027built to include Go support. GCC binary releases are available from
Suriyaa Sundararuban1041ac82018-06-13 07:06:04 +000028<a href="https://gcc.gnu.org/install/binaries.html">various
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070029websites</a> and are typically included as part of GNU/Linux
Alberto Donizetti77aea692019-09-07 16:31:36 +020030distributions. We expect that most people who build these binaries
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070031will 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
Alberto Donizetti77aea692019-09-07 16:31:36 +020041identical to Go 1.1. The GCC 4.8.2 release includes a complete Go
Ian Lance Taylor8d206d92013-11-06 13:58:35 -0800421.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
Alberto Donizetti77aea692019-09-07 16:31:36 +020051user libraries. The Go 1.4 runtime is not fully merged, but that
Ian Lance Taylor8668ac02015-06-15 11:13:57 -070052should 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
Alberto Donizetti77aea692019-09-07 16:31:36 +020057user libraries. The Go 1.6 runtime is not fully merged, but that
Ian Lance Taylorea736492017-01-24 09:32:29 -080058should 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
Alberto Donizetti77aea692019-09-07 16:31:36 +020063user libraries. As with earlier releases, the Go 1.8 runtime is not
Ian Lance Taylora9345302017-09-28 09:49:46 -070064fully merged, but that should not be visible to Go programs.
65</p>
66
67<p>
Alberto Donizetti77aea692019-09-07 16:31:36 +020068The GCC 8 releases include a complete implementation of the Go 1.10.1
69release. The Go 1.10 runtime has now been fully merged into the GCC
70development sources, and concurrent garbage collection is fully
71supported.
72</p>
73
74<p>
75The GCC 9 releases include a complete implementation of the Go 1.12.2
76release.
Ian Lance Taylorea736492017-01-24 09:32:29 -080077</p>
78
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070079<h2 id="Source_code">Source code</h2>
80
81<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070082If you cannot use a release, or prefer to build gccgo for
Daniel Martíe70e0a62019-09-18 17:13:46 +010083yourself,
Alberto Donizetti77aea692019-09-07 16:31:36 +020084the gccgo source code is accessible via Subversion. The
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070085GCC web site
Suriyaa Sundararuban1041ac82018-06-13 07:06:04 +000086has <a href="https://gcc.gnu.org/svn.html">instructions for getting the
Alberto Donizetti77aea692019-09-07 16:31:36 +020087GCC source code</a>. The gccgo source code is included. As a
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070088convenience, a stable version of the Go support is available in
89a branch of the main GCC code
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070090repository: <code>svn://gcc.gnu.org/svn/gcc/branches/gccgo</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070091This branch is periodically updated with stable Go compiler sources.
Ian Lance Taylor8c409002009-11-06 14:16:47 -080092</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -070093
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080094<p>
95Note that although <code>gcc.gnu.org</code> is the most convenient way
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -070096to get the source code for the Go frontend, it is not where the master
Alberto Donizetti77aea692019-09-07 16:31:36 +020097sources live. If you want to contribute changes to the Go frontend
Andrew Gerrandf78d50c2012-03-05 14:31:27 +110098compiler, see <a href="/doc/gccgo_contribute.html">Contributing to
Ian Lance Taylor8653acb2010-01-29 16:37:20 -080099gccgo</a>.
100</p>
101
102
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700103<h2 id="Building">Building</h2>
104
105<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700106Building gccgo is just like building GCC
Alberto Donizetti77aea692019-09-07 16:31:36 +0200107with one or two additional options. See
Suriyaa Sundararuban1041ac82018-06-13 07:06:04 +0000108the <a href="https://gcc.gnu.org/install/">instructions on the gcc web
Alberto Donizetti77aea692019-09-07 16:31:36 +0200109site</a>. When you run <code>configure</code>, add the
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -0800110option <code>--enable-languages=c,c++,go</code> (along with other
Alberto Donizetti77aea692019-09-07 16:31:36 +0200111languages you may want to build). If you are targeting a 32-bit x86,
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700112then you will want to build gccgo to default to
Ian Lance Taylor9e8d1362009-11-18 12:48:48 -0800113supporting locked compare and exchange instructions; do this by also
114using the <code>configure</code> option <code>--with-arch=i586</code>
115(or a newer architecture, depending on where you need your programs to
Alberto Donizetti77aea692019-09-07 16:31:36 +0200116run). If you are targeting a 64-bit x86, but sometimes want to use
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700117the <code>-m32</code> option, then use the <code>configure</code>
118option <code>--with-arch-32=i586</code>.
119</p>
120
121<h3 id="Gold">Gold</h3>
122
123<p>
124On x86 GNU/Linux systems the gccgo compiler is able to
Alberto Donizetti77aea692019-09-07 16:31:36 +0200125use a small discontiguous stack for goroutines. This permits programs
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700126to run many more goroutines, since each goroutine can use a relatively
Alberto Donizetti77aea692019-09-07 16:31:36 +0200127small stack. Doing this requires using the gold linker version 2.22
128or later. You can either install GNU binutils 2.22 or later, or you
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700129can build gold yourself.
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800130</p>
131
132<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700133To build gold yourself, build the GNU binutils,
134using <code>--enable-gold=default</code> when you run
Alberto Donizetti77aea692019-09-07 16:31:36 +0200135the <code>configure</code> script. Before building, you must install
136the flex and bison packages. A typical sequence would look like
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800137this (you can replace <code>/opt/gold</code> with any directory to
138which you have write access):
139</p>
140
141<pre>
142cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
143[password is "anoncvs"]
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700144[The next command will create a directory named src, not binutils]
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800145cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
146mkdir binutils-objdir
147cd binutils-objdir
Ian Lance Taylor5d1d0402011-09-19 11:54:07 -0700148../src/configure --enable-gold=default --prefix=/opt/gold
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800149make
150make install
151</pre>
152
153<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700154However you install gold, when you configure gccgo, use the
155option <code>--with-ld=<var>GOLD_BINARY</var></code>.
156</p>
157
158<h3 id="Prerequisites">Prerequisites</h3>
159
160<p>
161A number of prerequisites are required to build GCC, as
162described on
Suriyaa Sundararuban1041ac82018-06-13 07:06:04 +0000163the <a href="https://gcc.gnu.org/install/prerequisites.html">gcc web
Alberto Donizetti77aea692019-09-07 16:31:36 +0200164site</a>. It is important to install all the prerequisites before
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700165running the gcc <code>configure</code> script.
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700166The prerequisite libraries can be conveniently downloaded using the
167script <code>contrib/download_prerequisites</code> in the GCC sources.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700168
169<h3 id="Build_commands">Build commands</h3>
170
171<p>
172Once all the prerequisites are installed, then a typical build and
173install sequence would look like this (only use
174the <code>--with-ld</code> option if you are using the gold linker as
175described above):
Ian Lance Taylor1d5e1f52009-12-29 14:59:08 -0800176</p>
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800177
178<pre>
179svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo
180mkdir objdir
181cd objdir
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700182../gccgo/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --with-ld=/opt/gold/bin/ld
Ian Lance Taylor8c409002009-11-06 14:16:47 -0800183make
184make install
185</pre>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700186
187<h2 id="Using_gccgo">Using gccgo</h2>
188
189<p>
Alberto Donizetti77aea692019-09-07 16:31:36 +0200190The gccgo compiler works like other gcc frontends. As of GCC 5 the gccgo
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700191installation also includes a version of the <code>go</code> command,
192which may be used to build Go programs as described at
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600193<a href="https://golang.org/cmd/go">https://golang.org/cmd/go</a>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700194</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700195
196<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700197To compile a file without using the <code>go</code> command:
198</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700199
200<pre>
201gccgo -c file.go
202</pre>
203
204<p>
205That produces <code>file.o</code>. To link files together to form an
206executable:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700207</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700208
209<pre>
210gccgo -o file file.o
211</pre>
212
213<p>
214To run the resulting file, you will need to tell the program where to
Alberto Donizetti77aea692019-09-07 16:31:36 +0200215find the compiled Go packages. There are a few ways to do this:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700216</p>
217
218<ul>
219<li>
220<p>
221Set the <code>LD_LIBRARY_PATH</code> environment variable:
222</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700223
224<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700225LD_LIBRARY_PATH=${prefix}/lib/gcc/MACHINE/VERSION
226[or]
227LD_LIBRARY_PATH=${prefix}/lib64/gcc/MACHINE/VERSION
228export LD_LIBRARY_PATH
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700229</pre>
230
231<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700232Here <code>${prefix}</code> is the <code>--prefix</code> option used
Alberto Donizetti77aea692019-09-07 16:31:36 +0200233when building gccgo. For a binary install this is
234normally <code>/usr</code>. Whether to use <code>lib</code>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700235or <code>lib64</code> depends on the target.
236Typically <code>lib64</code> is correct for x86_64 systems,
Alberto Donizetti77aea692019-09-07 16:31:36 +0200237and <code>lib</code> is correct for other systems. The idea is to
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700238name the directory where <code>libgo.so</code> is found.
239</p>
240
241</li>
242
243<li>
244<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700245Passing a <code>-Wl,-R</code> option when you link (replace lib with
246lib64 if appropriate for your system):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700247</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700248
249<pre>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700250go build -gccgoflags -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700251[or]
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700252gccgo -o file file.o -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700253</pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700254</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-libgo</code> option to link statically against
259the compiled packages.
260</p>
261</li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700262
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700263<li>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700264<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700265Use the <code>-static</code> option to do a fully static link (the
266default for the <code>gc</code> compiler).
267</p>
268</li>
269</ul>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700270
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800271<h2 id="Options">Options</h2>
272
273<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700274The gccgo compiler supports all GCC options
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800275that are language independent, notably the <code>-O</code>
276and <code>-g</code> options.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700277</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800278
279<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700280The <code>-fgo-pkgpath=PKGPATH</code> option may be used to set a
281unique prefix for the package being compiled.
282This option is automatically used by the go command, but you may want
283to use it if you invoke gccgo directly.
284This option is intended for use with large
285programs that contain many packages, in order to allow multiple
286packages to use the same identifier as the package name.
287The <code>PKGPATH</code> may be any string; a good choice for the
288string is the path used to import the package.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700289</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800290
291<p>
292The <code>-I</code> and <code>-L</code> options, which are synonyms
293for the compiler, may be used to set the search path for finding
294imports.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700295These options are not needed if you build with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700296</p>
Ian Lance Taylor8653acb2010-01-29 16:37:20 -0800297
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700298<h2 id="Imports">Imports</h2>
299
300<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700301When you compile a file that exports something, the export
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700302information will be stored directly in the object file.
303If you build with gccgo directly, rather than with the go command,
304then when you import a package, you must tell gccgo how to find the
305file.
306</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700307
308<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700309When you import the package <var>FILE</var> with gccgo,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700310it will look for the import data in the following files, and use the
311first one that it finds.
312
313<ul>
314<li><code><var>FILE</var>.gox</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700315<li><code>lib<var>FILE</var>.so</code>
316<li><code>lib<var>FILE</var>.a</code>
Peter Collingbournea5cc5ba2013-12-06 13:33:10 -0800317<li><code><var>FILE</var>.o</code>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700318</ul>
319
320<p>
321<code><var>FILE</var>.gox</code>, when used, will typically contain
322nothing but export data. This can be generated from
323<code><var>FILE</var>.o</code> via
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700324</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700325
326<pre>
327objcopy -j .go_export FILE.o FILE.gox
328</pre>
329
330<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700331The gccgo compiler will look in the current
Alberto Donizetti77aea692019-09-07 16:31:36 +0200332directory for import files. In more complex scenarios you
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700333may pass the <code>-I</code> or <code>-L</code> option to
Alberto Donizetti77aea692019-09-07 16:31:36 +0200334gccgo. Both options take directories to search. The
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700335<code>-L</code> option is also passed to the linker.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700336</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700337
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700338<p>
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700339The gccgo compiler does not currently (2015-06-15) record
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700340the file name of imported packages in the object file. You must
341arrange for the imported data to be linked into the program.
Ian Lance Taylor8668ac02015-06-15 11:13:57 -0700342Again, this is not necessary when building with the go command.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700343</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700344
345<pre>
346gccgo -c mypackage.go # Exports mypackage
347gccgo -c main.go # Imports mypackage
348gccgo -o main main.o mypackage.o # Explicitly links with mypackage.o
349</pre>
350
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700351<h2 id="Debugging">Debugging</h2>
352
353<p>
354If you use the <code>-g</code> option when you compile, you can run
Alberto Donizetti77aea692019-09-07 16:31:36 +0200355<code>gdb</code> on your executable. The debugger has only limited
356knowledge about Go. You can set breakpoints, single-step,
357etc. You can print variables, but they will be printed as though they
358had C/C++ types. For numeric types this doesn't matter. Go strings
359and interfaces will show up as two-element structures. Go
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700360maps and channels are always represented as C pointers to run-time
361structures.
362</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700363
364<h2 id="C_Interoperability">C Interoperability</h2>
365
366<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700367When using gccgo there is limited interoperability with C,
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700368or with C++ code compiled using <code>extern "C"</code>.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700369</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700370
371<h3 id="Types">Types</h3>
372
373<p>
Ian Lance Taylorea736492017-01-24 09:32:29 -0800374Basic types map directly: an <code>int32</code> in Go is
375an <code>int32_t</code> in C, an <code>int64</code> is
376an <code>int64_t</code>, etc.
377The Go type <code>int</code> is an integer that is the same size as a
378pointer, and as such corresponds to the C type <code>intptr_t</code>.
379Go <code>byte</code> is equivalent to C <code>unsigned char</code>.
380Pointers in Go are pointers in C.
381A Go <code>struct</code> is the same as C <code>struct</code> with the
382same fields and types.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700383</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700384
385<p>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700386The Go <code>string</code> type is currently defined as a two-element
387structure (this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700388</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700389
390<pre>
391struct __go_string {
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700392 const unsigned char *__data;
Ian Lance Taylorea736492017-01-24 09:32:29 -0800393 intptr_t __length;
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700394};
395</pre>
396
397<p>
398You can't pass arrays between C and Go. However, a pointer to an
399array in Go is equivalent to a C pointer to the
400equivalent of the element type.
401For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>,
402assuming that the C pointer does point to 10 elements.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700403</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700404
405<p>
Alberto Donizetti77aea692019-09-07 16:31:36 +0200406A slice in Go is a structure. The current definition is
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700407(this is <b style="color: red;">subject to change</b>):
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700408</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700409
410<pre>
411struct __go_slice {
412 void *__values;
Ian Lance Taylorea736492017-01-24 09:32:29 -0800413 intptr_t __count;
414 intptr_t __capacity;
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700415};
416</pre>
417
418<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700419The type of a Go function is a pointer to a struct (this is
Alberto Donizetti77aea692019-09-07 16:31:36 +0200420<b style="color: red;">subject to change</b>). The first field in the
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700421struct points to the code of the function, which will be equivalent to
422a pointer to a C function whose parameter types are equivalent, with
Alberto Donizetti77aea692019-09-07 16:31:36 +0200423an additional trailing parameter. The trailing parameter is the
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700424closure, and the argument to pass is a pointer to the Go function
425struct.
426
427When a Go function returns more than one value, the C function returns
Alberto Donizetti77aea692019-09-07 16:31:36 +0200428a struct. For example, these functions are roughly equivalent:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700429</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700430
431<pre>
Rob Pike80e25fc2011-01-19 23:07:38 -0500432func GoFunction(int) (int, float64)
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700433struct { int i; float64 f; } CFunction(int, void*)
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700434</pre>
435
436<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700437Go <code>interface</code>, <code>channel</code>, and <code>map</code>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700438types have no corresponding C type (<code>interface</code> is a
439two-element struct and <code>channel</code> and <code>map</code> are
440pointers to structs in C, but the structs are deliberately undocumented). C
441<code>enum</code> types correspond to some integer type, but precisely
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700442which one is difficult to predict in general; use a cast. C <code>union</code>
443types have no corresponding Go type. C <code>struct</code> types containing
444bitfields have no corresponding Go type. C++ <code>class</code> types have
445no corresponding Go type.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700446</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700447
448<p>
449Memory allocation is completely different between C and Go, as Go uses
450garbage collection. The exact guidelines in this area are undetermined,
451but it is likely that it will be permitted to pass a pointer to allocated
452memory from C to Go. The responsibility of eventually freeing the pointer
453will remain with C side, and of course if the C side frees the pointer
454while the Go side still has a copy the program will fail. When passing a
455pointer from Go to C, the Go function must retain a visible copy of it in
456some Go variable. Otherwise the Go garbage collector may delete the
457pointer while the C function is still using it.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700458</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700459
460<h3 id="Function_names">Function names</h3>
461
462<p>
463Go code can call C functions directly using a Go extension implemented
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700464in gccgo: a function declaration may be preceded by
Alberto Donizetti77aea692019-09-07 16:31:36 +0200465<code>//extern NAME</code>. For example, here is how the C function
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700466<code>open</code> can be declared in Go:
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700467</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700468
469<pre>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700470//extern open
471func c_open(name *byte, mode int, perm int) int
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700472</pre>
473
474<p>
Jeremy Jackins7e054262012-03-19 08:26:36 +1100475The C function naturally expects a NUL-terminated string, which in
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700476Go is equivalent to a pointer to an array (not a slice!) of
477<code>byte</code> with a terminating zero byte. So a sample call
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700478from Go would look like (after importing the <code>syscall</code> package):
479</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700480
481<pre>
482var name = [4]byte{'f', 'o', 'o', 0};
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700483i := c_open(&amp;name[0], syscall.O_RDONLY, 0);
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700484</pre>
485
486<p>
487(this serves as an example only, to open a file in Go please use Go's
488<code>os.Open</code> function instead).
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700489</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700490
491<p>
Ian Lance Taylorffde4972013-06-20 14:49:15 -0700492Note that if the C function can block, such as in a call
493to <code>read</code>, calling the C function may block the Go program.
494Unless you have a clear understanding of what you are doing, all calls
495between C and Go should be implemented through cgo or SWIG, as for
496the <code>gc</code> compiler.
497</p>
498
499<p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700500The name of Go functions accessed from C is subject to change. At present
501the name of a Go function that does not have a receiver is
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700502<code>prefix.package.Functionname</code>. The prefix is set by
503the <code>-fgo-prefix</code> option used when the package is compiled;
Russ Coxae7497b2012-02-06 13:34:35 -0500504if the option is not used, the default is <code>go</code>.
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700505To call the function from C you must set the name using
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700506a GCC extension.
507</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700508
509<pre>
Ian Lance Taylor659966a2010-08-23 17:50:30 -0700510extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700511</pre>
512
513<h3 id="Automatic_generation_of_Go_declarations_from_C_source_code">
514Automatic generation of Go declarations from C source code</h3>
515
516<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700517The Go version of GCC supports automatically generating
518Go declarations from C code. The facility is rather awkward, and most
519users should use the <a href="/cmd/cgo">cgo</a> program with
520the <code>-gccgo</code> option instead.
521</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700522
523<p>
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700524Compile your C code as usual, and add the option
Alberto Donizetti77aea692019-09-07 16:31:36 +0200525<code>-fdump-go-spec=<var>FILENAME</var></code>. This will create the
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700526file <code><var>FILENAME</var></code> as a side effect of the
Alberto Donizetti77aea692019-09-07 16:31:36 +0200527compilation. This file will contain Go declarations for the types,
528variables and functions declared in the C code. C types that can not
529be represented in Go will be recorded as comments in the Go code. The
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700530generated file will not have a <code>package</code> declaration, but
531can otherwise be compiled directly by gccgo.
532</p>
Ian Lance Taylor4262ff02009-10-14 15:49:20 -0700533
534<p>
535This procedure is full of unstated caveats and restrictions and we make no
536guarantee that it will not change in the future. It is more useful as a
537starting point for real Go code than as a regular procedure.
Ian Lance Taylora84e3ba2012-03-20 14:16:34 -0700538</p>