Home | History | Annotate | Download | only in doc
      1 <!--{
      2 	"Title": "Setting up and using gccgo",
      3 	"Path": "/doc/install/gccgo"
      4 }-->
      5 
      6 <p>
      7 This document explains how to use gccgo, a compiler for
      8 the Go language.  The gccgo compiler is a new frontend
      9 for GCC, the widely used GNU compiler.  Although the
     10 frontend itself is under a BSD-style license, gccgo is
     11 normally used as part of GCC and is then covered by
     12 the <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public
     13 License</a> (the license covers gccgo itself as part of GCC; it
     14 does not cover code generated by gccgo).
     15 </p>
     16 
     17 <p>
     18 Note that gccgo is not the <code>gc</code> compiler; see
     19 the <a href="/doc/install.html">Installing Go</a> instructions for that
     20 compiler.
     21 </p>
     22 
     23 <h2 id="Releases">Releases</h2>
     24 
     25 <p>
     26 The simplest way to install gccgo is to install a GCC binary release
     27 built to include Go support.  GCC binary releases are available from
     28 <a href="http://gcc.gnu.org/install/binaries.html">various
     29 websites</a> and are typically included as part of GNU/Linux
     30 distributions.  We expect that most people who build these binaries
     31 will include Go support.
     32 </p>
     33 
     34 <p>
     35 The 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>
     40 Due to timing, the GCC 4.8.0 and 4.8.1 releases are close to but not
     41 identical to Go 1.1.  The GCC 4.8.2 release includes a complete Go
     42 1.1.2 implementation.
     43 </p>
     44 
     45 <p>
     46 The GCC 4.9 releases include a complete Go 1.2 implementation.
     47 </p>
     48 
     49 <p>
     50 The GCC 5 releases include a complete implementation of the Go 1.4
     51 user libraries.  The Go 1.4 runtime is not fully merged, but that
     52 should not be visible to Go programs.
     53 </p>
     54 
     55 <p>
     56 The GCC 6 releases include a complete implementation of the Go 1.6.1
     57 user libraries.  The Go 1.6 runtime is not fully merged, but that
     58 should not be visible to Go programs.
     59 </p>
     60 
     61 <p>
     62 The GCC 7 releases are expected to include a complete implementation
     63 of the Go 1.8 user libraries.  As with earlier releases, the Go 1.8
     64 runtime is not fully merged, but that should not be visible to Go
     65 programs.
     66 </p>
     67 
     68 <h2 id="Source_code">Source code</h2>
     69 
     70 <p>
     71 If you cannot use a release, or prefer to build gccgo for
     72 yourself, 
     73 the gccgo source code is accessible via Subversion.  The
     74 GCC web site
     75 has <a href="http://gcc.gnu.org/svn.html">instructions for getting the
     76 GCC source code</a>.  The gccgo source code is included.  As a
     77 convenience, a stable version of the Go support is available in
     78 a branch of the main GCC code
     79 repository: <code>svn://gcc.gnu.org/svn/gcc/branches/gccgo</code>.
     80 This branch is periodically updated with stable Go compiler sources.
     81 </p>
     82 
     83 <p>
     84 Note that although <code>gcc.gnu.org</code> is the most convenient way
     85 to get the source code for the Go frontend, it is not where the master
     86 sources live.  If you want to contribute changes to the Go frontend
     87 compiler, see <a href="/doc/gccgo_contribute.html">Contributing to
     88 gccgo</a>.
     89 </p>
     90 
     91 
     92 <h2 id="Building">Building</h2>
     93 
     94 <p>
     95 Building gccgo is just like building GCC
     96 with one or two additional options.  See
     97 the <a href="http://gcc.gnu.org/install/">instructions on the gcc web
     98 site</a>.  When you run <code>configure</code>, add the
     99 option <code>--enable-languages=c,c++,go</code> (along with other
    100 languages you may want to build).  If you are targeting a 32-bit x86,
    101 then you will want to build gccgo to default to
    102 supporting locked compare and exchange instructions; do this by also
    103 using the <code>configure</code> option <code>--with-arch=i586</code>
    104 (or a newer architecture, depending on where you need your programs to
    105 run).  If you are targeting a 64-bit x86, but sometimes want to use
    106 the <code>-m32</code> option, then use the <code>configure</code>
    107 option <code>--with-arch-32=i586</code>.
    108 </p>
    109 
    110 <h3 id="Gold">Gold</h3>
    111 
    112 <p>
    113 On x86 GNU/Linux systems the gccgo compiler is able to
    114 use a small discontiguous stack for goroutines.  This permits programs
    115 to run many more goroutines, since each goroutine can use a relatively
    116 small stack.  Doing this requires using the gold linker version 2.22
    117 or later.  You can either install GNU binutils 2.22 or later, or you
    118 can build gold yourself.
    119 </p>
    120 
    121 <p>
    122 To build gold yourself, build the GNU binutils,
    123 using <code>--enable-gold=default</code> when you run
    124 the <code>configure</code> script.  Before building, you must install
    125 the flex and bison packages.  A typical sequence would look like
    126 this (you can replace <code>/opt/gold</code> with any directory to
    127 which you have write access):
    128 </p>
    129 
    130 <pre>
    131 cvs -z 9 -d :pserver:anoncvs (a] sourceware.org:/cvs/src login
    132 [password is "anoncvs"]
    133 [The next command will create a directory named src, not binutils]
    134 cvs -z 9 -d :pserver:anoncvs (a] sourceware.org:/cvs/src co binutils
    135 mkdir binutils-objdir
    136 cd binutils-objdir
    137 ../src/configure --enable-gold=default --prefix=/opt/gold
    138 make
    139 make install
    140 </pre>
    141 
    142 <p>
    143 However you install gold, when you configure gccgo, use the
    144 option <code>--with-ld=<var>GOLD_BINARY</var></code>.
    145 </p>
    146 
    147 <h3 id="Prerequisites">Prerequisites</h3>
    148 
    149 <p>
    150 A number of prerequisites are required to build GCC, as
    151 described on
    152 the <a href="http://gcc.gnu.org/install/prerequisites.html">gcc web
    153 site</a>.  It is important to install all the prerequisites before
    154 running the gcc <code>configure</code> script.
    155 The prerequisite libraries can be conveniently downloaded using the
    156 script <code>contrib/download_prerequisites</code> in the GCC sources.
    157 
    158 <h3 id="Build_commands">Build commands</h3>
    159 
    160 <p>
    161 Once all the prerequisites are installed, then a typical build and
    162 install sequence would look like this (only use
    163 the <code>--with-ld</code> option if you are using the gold linker as
    164 described above):
    165 </p>
    166 
    167 <pre>
    168 svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo
    169 mkdir objdir
    170 cd objdir
    171 ../gccgo/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --with-ld=/opt/gold/bin/ld
    172 make
    173 make install
    174 </pre>
    175 
    176 <h2 id="Using_gccgo">Using gccgo</h2>
    177 
    178 <p>
    179 The gccgo compiler works like other gcc frontends.  As of GCC 5 the gccgo
    180 installation also includes a version of the <code>go</code> command,
    181 which may be used to build Go programs as described at
    182 <a href="https://golang.org/cmd/go">https://golang.org/cmd/go</a>.
    183 </p>
    184 
    185 <p>
    186 To compile a file without using the <code>go</code> command:
    187 </p>
    188 
    189 <pre>
    190 gccgo -c file.go
    191 </pre>
    192 
    193 <p>
    194 That produces <code>file.o</code>. To link files together to form an
    195 executable:
    196 </p>
    197 
    198 <pre>
    199 gccgo -o file file.o
    200 </pre>
    201 
    202 <p>
    203 To run the resulting file, you will need to tell the program where to
    204 find the compiled Go packages.  There are a few ways to do this:
    205 </p>
    206 
    207 <ul>
    208 <li>
    209 <p>
    210 Set the <code>LD_LIBRARY_PATH</code> environment variable:
    211 </p>
    212 
    213 <pre>
    214 LD_LIBRARY_PATH=${prefix}/lib/gcc/MACHINE/VERSION
    215 [or]
    216 LD_LIBRARY_PATH=${prefix}/lib64/gcc/MACHINE/VERSION
    217 export LD_LIBRARY_PATH
    218 </pre>
    219 
    220 <p>
    221 Here <code>${prefix}</code> is the <code>--prefix</code> option used
    222 when building gccgo.  For a binary install this is
    223 normally <code>/usr</code>.  Whether to use <code>lib</code>
    224 or <code>lib64</code> depends on the target.
    225 Typically <code>lib64</code> is correct for x86_64 systems,
    226 and <code>lib</code> is correct for other systems.  The idea is to
    227 name the directory where <code>libgo.so</code> is found.
    228 </p>
    229 
    230 </li>
    231 
    232 <li>
    233 <p>
    234 Passing a <code>-Wl,-R</code> option when you link (replace lib with
    235 lib64 if appropriate for your system):
    236 </p>
    237 
    238 <pre>
    239 go build -gccgoflags -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
    240 [or]
    241 gccgo -o file file.o -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION
    242 </pre>
    243 </li>
    244 
    245 <li>
    246 <p>
    247 Use the <code>-static-libgo</code> option to link statically against
    248 the compiled packages.
    249 </p>
    250 </li>
    251 
    252 <li>
    253 <p>
    254 Use the <code>-static</code> option to do a fully static link (the
    255 default for the <code>gc</code> compiler).
    256 </p>
    257 </li>
    258 </ul>
    259 
    260 <h2 id="Options">Options</h2>
    261 
    262 <p>
    263 The gccgo compiler supports all GCC options
    264 that are language independent, notably the <code>-O</code>
    265 and <code>-g</code> options.
    266 </p>
    267 
    268 <p>
    269 The <code>-fgo-pkgpath=PKGPATH</code> option may be used to set a
    270 unique prefix for the package being compiled.
    271 This option is automatically used by the go command, but you may want
    272 to use it if you invoke gccgo directly.
    273 This option is intended for use with large
    274 programs that contain many packages, in order to allow multiple
    275 packages to use the same identifier as the package name.
    276 The <code>PKGPATH</code> may be any string; a good choice for the
    277 string is the path used to import the package.
    278 </p>
    279 
    280 <p>
    281 The <code>-I</code> and <code>-L</code> options, which are synonyms
    282 for the compiler, may be used to set the search path for finding
    283 imports.
    284 These options are not needed if you build with the go command.
    285 </p>
    286 
    287 <h2 id="Imports">Imports</h2>
    288 
    289 <p>
    290 When you compile a file that exports something, the export
    291 information will be stored directly in the object file.
    292 If you build with gccgo directly, rather than with the go command,
    293 then when you import a package, you must tell gccgo how to find the
    294 file.
    295 </p>
    296 
    297 <p>
    298 When you import the package <var>FILE</var> with gccgo,
    299 it will look for the import data in the following files, and use the
    300 first one that it finds.
    301 
    302 <ul>
    303 <li><code><var>FILE</var>.gox</code>
    304 <li><code>lib<var>FILE</var>.so</code>
    305 <li><code>lib<var>FILE</var>.a</code>
    306 <li><code><var>FILE</var>.o</code>
    307 </ul>
    308 
    309 <p>
    310 <code><var>FILE</var>.gox</code>, when used, will typically contain
    311 nothing but export data. This can be generated from
    312 <code><var>FILE</var>.o</code> via
    313 </p>
    314 
    315 <pre>
    316 objcopy -j .go_export FILE.o FILE.gox
    317 </pre>
    318 
    319 <p>
    320 The gccgo compiler will look in the current
    321 directory for import files.  In more complex scenarios you
    322 may pass the <code>-I</code> or <code>-L</code> option to
    323 gccgo.  Both options take directories to search. The
    324 <code>-L</code> option is also passed to the linker.
    325 </p>
    326 
    327 <p>
    328 The gccgo compiler does not currently (2015-06-15) record
    329 the file name of imported packages in the object file. You must
    330 arrange for the imported data to be linked into the program.
    331 Again, this is not necessary when building with the go command.
    332 </p>
    333 
    334 <pre>
    335 gccgo -c mypackage.go              # Exports mypackage
    336 gccgo -c main.go                   # Imports mypackage
    337 gccgo -o main main.o mypackage.o   # Explicitly links with mypackage.o
    338 </pre>
    339 
    340 <h2 id="Debugging">Debugging</h2>
    341 
    342 <p>
    343 If you use the <code>-g</code> option when you compile, you can run
    344 <code>gdb</code> on your executable.  The debugger has only limited
    345 knowledge about Go.  You can set breakpoints, single-step,
    346 etc.  You can print variables, but they will be printed as though they
    347 had C/C++ types.  For numeric types this doesn't matter.  Go strings
    348 and interfaces will show up as two-element structures.  Go
    349 maps and channels are always represented as C pointers to run-time
    350 structures.
    351 </p>
    352 
    353 <h2 id="C_Interoperability">C Interoperability</h2>
    354 
    355 <p>
    356 When using gccgo there is limited interoperability with C,
    357 or with C++ code compiled using <code>extern "C"</code>.
    358 </p>
    359 
    360 <h3 id="Types">Types</h3>
    361 
    362 <p>
    363 Basic types map directly: an <code>int32</code> in Go is
    364 an <code>int32_t</code> in C, an <code>int64</code> is
    365 an <code>int64_t</code>, etc.
    366 The Go type <code>int</code> is an integer that is the same size as a
    367 pointer, and as such corresponds to the C type <code>intptr_t</code>.
    368 Go <code>byte</code> is equivalent to C <code>unsigned char</code>.
    369 Pointers in Go are pointers in C.
    370 A Go <code>struct</code> is the same as C <code>struct</code> with the
    371 same fields and types.
    372 </p>
    373 
    374 <p>
    375 The Go <code>string</code> type is currently defined as a two-element
    376 structure (this is <b style="color: red;">subject to change</b>):
    377 </p>
    378 
    379 <pre>
    380 struct __go_string {
    381   const unsigned char *__data;
    382   intptr_t __length;
    383 };
    384 </pre>
    385 
    386 <p>
    387 You can't pass arrays between C and Go. However, a pointer to an
    388 array in Go is equivalent to a C pointer to the
    389 equivalent of the element type.
    390 For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>,
    391 assuming that the C pointer does point to 10 elements.
    392 </p>
    393 
    394 <p>
    395 A slice in Go is a structure.  The current definition is
    396 (this is <b style="color: red;">subject to change</b>):
    397 </p>
    398 
    399 <pre>
    400 struct __go_slice {
    401   void *__values;
    402   intptr_t __count;
    403   intptr_t __capacity;
    404 };
    405 </pre>
    406 
    407 <p>
    408 The type of a Go function is a pointer to a struct (this is
    409 <b style="color: red;">subject to change</b>).  The first field in the
    410 struct points to the code of the function, which will be equivalent to
    411 a pointer to a C function whose parameter types are equivalent, with
    412 an additional trailing parameter.  The trailing parameter is the
    413 closure, and the argument to pass is a pointer to the Go function
    414 struct.
    415 
    416 When a Go function returns more than one value, the C function returns
    417 a struct.  For example, these functions are roughly equivalent:
    418 </p>
    419 
    420 <pre>
    421 func GoFunction(int) (int, float64)
    422 struct { int i; float64 f; } CFunction(int, void*)
    423 </pre>
    424 
    425 <p>
    426 Go <code>interface</code>, <code>channel</code>, and <code>map</code>
    427 types have no corresponding C type (<code>interface</code> is a
    428 two-element struct and <code>channel</code> and <code>map</code> are
    429 pointers to structs in C, but the structs are deliberately undocumented). C
    430 <code>enum</code> types correspond to some integer type, but precisely
    431 which one is difficult to predict in general; use a cast. C <code>union</code>
    432 types have no corresponding Go type. C <code>struct</code> types containing
    433 bitfields have no corresponding Go type. C++ <code>class</code> types have
    434 no corresponding Go type.
    435 </p>
    436 
    437 <p>
    438 Memory allocation is completely different between C and Go, as Go uses
    439 garbage collection. The exact guidelines in this area are undetermined,
    440 but it is likely that it will be permitted to pass a pointer to allocated
    441 memory from C to Go. The responsibility of eventually freeing the pointer
    442 will remain with C side, and of course if the C side frees the pointer
    443 while the Go side still has a copy the program will fail. When passing a
    444 pointer from Go to C, the Go function must retain a visible copy of it in
    445 some Go variable. Otherwise the Go garbage collector may delete the
    446 pointer while the C function is still using it.
    447 </p>
    448 
    449 <h3 id="Function_names">Function names</h3>
    450 
    451 <p>
    452 Go code can call C functions directly using a Go extension implemented
    453 in gccgo: a function declaration may be preceded by
    454 <code>//extern NAME</code>.  For example, here is how the C function
    455 <code>open</code> can be declared in Go:
    456 </p>
    457 
    458 <pre>
    459 //extern open
    460 func c_open(name *byte, mode int, perm int) int
    461 </pre>
    462 
    463 <p>
    464 The C function naturally expects a NUL-terminated string, which in
    465 Go is equivalent to a pointer to an array (not a slice!) of
    466 <code>byte</code> with a terminating zero byte. So a sample call
    467 from Go would look like (after importing the <code>syscall</code> package):
    468 </p>
    469 
    470 <pre>
    471 var name = [4]byte{'f', 'o', 'o', 0};
    472 i := c_open(&amp;name[0], syscall.O_RDONLY, 0);
    473 </pre>
    474 
    475 <p>
    476 (this serves as an example only, to open a file in Go please use Go's
    477 <code>os.Open</code> function instead).
    478 </p>
    479 
    480 <p>
    481 Note that if the C function can block, such as in a call
    482 to <code>read</code>, calling the C function may block the Go program.
    483 Unless you have a clear understanding of what you are doing, all calls
    484 between C and Go should be implemented through cgo or SWIG, as for
    485 the <code>gc</code> compiler.
    486 </p>
    487 
    488 <p>
    489 The name of Go functions accessed from C is subject to change. At present
    490 the name of a Go function that does not have a receiver is
    491 <code>prefix.package.Functionname</code>. The prefix is set by
    492 the <code>-fgo-prefix</code> option used when the package is compiled;
    493 if the option is not used, the default is <code>go</code>.
    494 To call the function from C you must set the name using
    495 a GCC extension.
    496 </p>
    497 
    498 <pre>
    499 extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
    500 </pre>
    501 
    502 <h3 id="Automatic_generation_of_Go_declarations_from_C_source_code">
    503 Automatic generation of Go declarations from C source code</h3>
    504 
    505 <p>
    506 The Go version of GCC supports automatically generating
    507 Go declarations from C code. The facility is rather awkward, and most
    508 users should use the <a href="/cmd/cgo">cgo</a> program with
    509 the <code>-gccgo</code> option instead.
    510 </p>
    511 
    512 <p>
    513 Compile your C code as usual, and add the option
    514 <code>-fdump-go-spec=<var>FILENAME</var></code>.  This will create the
    515 file <code><var>FILENAME</var></code> as a side effect of the
    516 compilation.  This file will contain Go declarations for the types,
    517 variables and functions declared in the C code.  C types that can not
    518 be represented in Go will be recorded as comments in the Go code.  The
    519 generated file will not have a <code>package</code> declaration, but
    520 can otherwise be compiled directly by gccgo.
    521 </p>
    522 
    523 <p>
    524 This procedure is full of unstated caveats and restrictions and we make no
    525 guarantee that it will not change in the future. It is more useful as a
    526 starting point for real Go code than as a regular procedure.
    527 </p>
    528