| 
									
										
										
										
											2012-01-19 11:24:54 +11:00
										 |  |  | <!--{
 | 
					
						
							| 
									
										
										
										
											2012-02-14 17:02:28 +11:00
										 |  |  | 	"Title": "Setting up and using gccgo", | 
					
						
							| 
									
										
										
										
											2012-03-05 14:31:27 +11:00
										 |  |  | 	"Path": "/doc/install/gccgo" | 
					
						
							| 
									
										
										
										
											2012-01-19 11:24:54 +11:00
										 |  |  | }--> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | This document explains how to use gccgo, a compiler for | 
					
						
							|  |  |  | the Go language.  The gccgo compiler is a new frontend | 
					
						
							|  |  |  | for GCC, the widely used GNU compiler.  Although the | 
					
						
							|  |  |  | frontend itself is under a BSD-style license, gccgo is | 
					
						
							|  |  |  | normally used as part of GCC and is then covered by | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | the <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | License</a> (the license covers gccgo itself as part of GCC; it | 
					
						
							|  |  |  | does not cover code generated by gccgo). | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Note that gccgo is not the <code>gc</code> compiler; see | 
					
						
							|  |  |  | the <a href="/doc/install.html">Installing Go</a> instructions for that | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | compiler. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <h2 id="Releases">Releases</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | The simplest way to install gccgo is to install a GCC binary release | 
					
						
							|  |  |  | built to include Go support.  GCC binary releases are available from | 
					
						
							|  |  |  | <a href="http://gcc.gnu.org/install/binaries.html">various | 
					
						
							|  |  |  | websites</a> and are typically included as part of GNU/Linux | 
					
						
							|  |  |  | distributions.  We expect that most people who build these binaries | 
					
						
							|  |  |  | will include Go support. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | The GCC 4.7.1 release and all later 4.7 releases include a complete | 
					
						
							|  |  |  | <a href="/doc/go1.html">Go 1</a> compiler and libraries. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Due to timing, the GCC 4.8.0 and 4.8.1 releases are close to but not | 
					
						
							| 
									
										
										
										
											2013-11-06 13:58:35 -08:00
										 |  |  | identical to Go 1.1.  The GCC 4.8.2 release includes a complete Go | 
					
						
							|  |  |  | 1.1.2 implementation. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <h2 id="Source_code">Source code</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | If you cannot use a release, or prefer to build gccgo for | 
					
						
							|  |  |  | yourself,  | 
					
						
							|  |  |  | the gccgo source code is accessible via Subversion.  The | 
					
						
							|  |  |  | GCC web site | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | has <a href="http://gcc.gnu.org/svn.html">instructions for getting the | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | GCC source code</a>.  The gccgo source code is included.  As a | 
					
						
							|  |  |  | convenience, a stable version of the Go support is available in | 
					
						
							|  |  |  | a branch of the main GCC code | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | repository: <code>svn://gcc.gnu.org/svn/gcc/branches/gccgo</code>. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | This branch is periodically updated with stable Go compiler sources. | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | <p> | 
					
						
							|  |  |  | Note that although <code>gcc.gnu.org</code> is the most convenient way | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | to get the source code for the Go frontend, it is not where the master | 
					
						
							|  |  |  | sources live.  If you want to contribute changes to the Go frontend | 
					
						
							| 
									
										
										
										
											2012-03-05 14:31:27 +11:00
										 |  |  | compiler, see <a href="/doc/gccgo_contribute.html">Contributing to | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | gccgo</a>. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <h2 id="Building">Building</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Building gccgo is just like building GCC | 
					
						
							| 
									
										
										
										
											2009-11-18 12:48:48 -08:00
										 |  |  | with one or two additional options.  See | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | the <a href="http://gcc.gnu.org/install/">instructions on the gcc web | 
					
						
							|  |  |  | site</a>.  When you run <code>configure</code>, add the | 
					
						
							| 
									
										
										
										
											2009-11-18 12:48:48 -08:00
										 |  |  | option <code>--enable-languages=c,c++,go</code> (along with other | 
					
						
							|  |  |  | languages you may want to build).  If you are targeting a 32-bit x86, | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | then you will want to build gccgo to default to | 
					
						
							| 
									
										
										
										
											2009-11-18 12:48:48 -08:00
										 |  |  | supporting locked compare and exchange instructions; do this by also | 
					
						
							|  |  |  | using the <code>configure</code> option <code>--with-arch=i586</code> | 
					
						
							|  |  |  | (or a newer architecture, depending on where you need your programs to | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | run).  If you are targeting a 64-bit x86, but sometimes want to use | 
					
						
							|  |  |  | the <code>-m32</code> option, then use the <code>configure</code> | 
					
						
							|  |  |  | option <code>--with-arch-32=i586</code>. | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <h3 id="Gold">Gold</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | On x86 GNU/Linux systems the gccgo compiler is able to | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | use a small discontiguous stack for goroutines.  This permits programs | 
					
						
							|  |  |  | to run many more goroutines, since each goroutine can use a relatively | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | small stack.  Doing this requires using the gold linker version 2.22 | 
					
						
							|  |  |  | or later.  You can either install GNU binutils 2.22 or later, or you | 
					
						
							|  |  |  | can build gold yourself. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | To build gold yourself, build the GNU binutils, | 
					
						
							|  |  |  | using <code>--enable-gold=default</code> when you run | 
					
						
							|  |  |  | the <code>configure</code> script.  Before building, you must install | 
					
						
							|  |  |  | the flex and bison packages.  A typical sequence would look like | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | this (you can replace <code>/opt/gold</code> with any directory to | 
					
						
							|  |  |  | which you have write access): | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login | 
					
						
							|  |  |  | [password is "anoncvs"] | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | [The next command will create a directory named src, not binutils] | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils | 
					
						
							|  |  |  | mkdir binutils-objdir | 
					
						
							|  |  |  | cd binutils-objdir | 
					
						
							| 
									
										
										
										
											2011-09-19 11:54:07 -07:00
										 |  |  | ../src/configure --enable-gold=default --prefix=/opt/gold | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | make | 
					
						
							|  |  |  | make install | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | However you install gold, when you configure gccgo, use the | 
					
						
							|  |  |  | option <code>--with-ld=<var>GOLD_BINARY</var></code>. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h3 id="Prerequisites">Prerequisites</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | A number of prerequisites are required to build GCC, as | 
					
						
							|  |  |  | described on | 
					
						
							|  |  |  | the <a href="http://gcc.gnu.org/install/prerequisites.html">gcc web | 
					
						
							|  |  |  | site</a>.  It is important to install all the prerequisites before | 
					
						
							|  |  |  | running the gcc <code>configure</code> script. | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | The prerequisite libraries can be conveniently downloaded using the | 
					
						
							|  |  |  | script <code>contrib/download_prerequisites</code> in the GCC sources. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <h3 id="Build_commands">Build commands</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Once all the prerequisites are installed, then a typical build and | 
					
						
							|  |  |  | install sequence would look like this (only use | 
					
						
							|  |  |  | the <code>--with-ld</code> option if you are using the gold linker as | 
					
						
							|  |  |  | described above): | 
					
						
							| 
									
										
										
										
											2009-12-29 14:59:08 -08:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo | 
					
						
							|  |  |  | mkdir objdir | 
					
						
							|  |  |  | cd objdir | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | ../gccgo/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --with-ld=/opt/gold/bin/ld | 
					
						
							| 
									
										
										
										
											2009-11-06 14:16:47 -08:00
										 |  |  | make | 
					
						
							|  |  |  | make install | 
					
						
							|  |  |  | </pre> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <h3 id="Ubuntu">A note on Ubuntu</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | Current versions of Ubuntu and versions of GCC before 4.8 disagree on | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | where system libraries and header files are found.  This is not a | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | gccgo issue.  When building older versions of GCC, setting these | 
					
						
							|  |  |  | environment variables while configuring and building gccgo may fix the | 
					
						
							|  |  |  | problem. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | LIBRARY_PATH=/usr/lib/x86_64-linux-gnu | 
					
						
							|  |  |  | C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu | 
					
						
							|  |  |  | CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu | 
					
						
							|  |  |  | export LIBRARY_PATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <h2 id="Using_gccgo">Using gccgo</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | The gccgo compiler works like other gcc frontends.  The gccgo | 
					
						
							|  |  |  | installation does not currently include a version of | 
					
						
							|  |  |  | the <code>go</code> command.  However if you have the <code>go</code> | 
					
						
							|  |  |  | command from an installation of the <code>gc</code> compiler, you can | 
					
						
							|  |  |  | use it with gccgo by passing the option <code>-compiler gccgo</code> | 
					
						
							|  |  |  | to <code>go build</code> or <code>go install</code> or <code>go | 
					
						
							|  |  |  | test</code>. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | To compile a file without using the <code>go</code> command: | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | gccgo -c file.go | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | That produces <code>file.o</code>. To link files together to form an | 
					
						
							|  |  |  | executable: | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | gccgo -o file file.o | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | To run the resulting file, you will need to tell the program where to | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | find the compiled Go packages.  There are a few ways to do this: | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  | <li> | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Set the <code>LD_LIBRARY_PATH</code> environment variable: | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | LD_LIBRARY_PATH=${prefix}/lib/gcc/MACHINE/VERSION | 
					
						
							|  |  |  | [or] | 
					
						
							|  |  |  | LD_LIBRARY_PATH=${prefix}/lib64/gcc/MACHINE/VERSION | 
					
						
							|  |  |  | export LD_LIBRARY_PATH | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Here <code>${prefix}</code> is the <code>--prefix</code> option used | 
					
						
							|  |  |  | when building gccgo.  For a binary install this is | 
					
						
							|  |  |  | normally <code>/usr</code>.  Whether to use <code>lib</code> | 
					
						
							|  |  |  | or <code>lib64</code> depends on the target. | 
					
						
							|  |  |  | Typically <code>lib64</code> is correct for x86_64 systems, | 
					
						
							|  |  |  | and <code>lib</code> is correct for other systems.  The idea is to | 
					
						
							|  |  |  | name the directory where <code>libgo.so</code> is found. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </li> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <li> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Passing a <code>-Wl,-R</code> option when you link: | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | gccgo -o file file.o -Wl,-R,${prefix}/lib/gcc/MACHINE/VERSION | 
					
						
							|  |  |  | [or] | 
					
						
							|  |  |  | gccgo -o file file.o -Wl,-R,${prefix}/lib64/gcc/MACHINE/VERSION | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </li> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <li> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Use the <code>-static-libgo</code> option to link statically against | 
					
						
							|  |  |  | the compiled packages. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | </li> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <li> | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Use the <code>-static</code> option to do a fully static link (the | 
					
						
							|  |  |  | default for the <code>gc</code> compiler). | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | </li> | 
					
						
							|  |  |  | </ul> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | <h2 id="Options">Options</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | The gccgo compiler supports all GCC options | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | that are language independent, notably the <code>-O</code> | 
					
						
							|  |  |  | and <code>-g</code> options. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | The <code>-fgo-prefix=PREFIX</code> option may be used to set a unique | 
					
						
							|  |  |  | prefix for the package being compiled.  This option is intended for | 
					
						
							|  |  |  | use with large programs that contain many packages, in order to allow | 
					
						
							|  |  |  | multiple packages to use the same identifier as the package name. | 
					
						
							|  |  |  | The <code>PREFIX</code> may be any string; a good choice for the | 
					
						
							|  |  |  | string is the directory where the package will be installed. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | The <code>-I</code> and <code>-L</code> options, which are synonyms | 
					
						
							|  |  |  | for the compiler, may be used to set the search path for finding | 
					
						
							|  |  |  | imports. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2010-01-29 16:37:20 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <h2 id="Imports">Imports</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | When you compile a file that exports something, the export | 
					
						
							|  |  |  | information will be stored directly in the object file.  When | 
					
						
							|  |  |  | you import a package, you must tell gccgo how to | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | find the file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | When you import the package <var>FILE</var> with gccgo, | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | it will look for the import data in the following files, and use the | 
					
						
							|  |  |  | first one that it finds. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  | <li><code><var>FILE</var>.gox</code> | 
					
						
							|  |  |  | <li><code>lib<var>FILE</var>.so</code> | 
					
						
							|  |  |  | <li><code>lib<var>FILE</var>.a</code> | 
					
						
							| 
									
										
										
										
											2013-12-06 13:33:10 -08:00
										 |  |  | <li><code><var>FILE</var>.o</code> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </ul> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | <code><var>FILE</var>.gox</code>, when used, will typically contain | 
					
						
							|  |  |  | nothing but export data. This can be generated from | 
					
						
							|  |  |  | <code><var>FILE</var>.o</code> via | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | objcopy -j .go_export FILE.o FILE.gox | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | The gccgo compiler will look in the current | 
					
						
							|  |  |  | directory for import files.  In more complex scenarios you | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | may pass the <code>-I</code> or <code>-L</code> option to | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | gccgo.  Both options take directories to search. The | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <code>-L</code> option is also passed to the linker. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | The gccgo compiler does not currently (2013-06-20) record | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | the file name of imported packages in the object file. You must | 
					
						
							|  |  |  | arrange for the imported data to be linked into the program. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | gccgo -c mypackage.go              # Exports mypackage | 
					
						
							|  |  |  | gccgo -c main.go                   # Imports mypackage | 
					
						
							|  |  |  | gccgo -o main main.o mypackage.o   # Explicitly links with mypackage.o | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h2 id="Debugging">Debugging</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | If you use the <code>-g</code> option when you compile, you can run | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | <code>gdb</code> on your executable.  The debugger has only limited | 
					
						
							|  |  |  | knowledge about Go.  You can set breakpoints, single-step, | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | etc.  You can print variables, but they will be printed as though they | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | had C/C++ types.  For numeric types this doesn't matter.  Go strings | 
					
						
							|  |  |  | and interfaces will show up as two-element structures.  Go | 
					
						
							|  |  |  | maps and channels are always represented as C pointers to run-time | 
					
						
							|  |  |  | structures. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <h2 id="C_Interoperability">C Interoperability</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | When using gccgo there is limited interoperability with C, | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | or with C++ code compiled using <code>extern "C"</code>. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <h3 id="Types">Types</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Basic types map directly: an <code>int</code> in Go is an <code>int</code> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | in C, an <code>int32</code> is an <code>int32_t</code>, | 
					
						
							|  |  |  | etc.  Go <code>byte</code> is equivalent to C <code>unsigned | 
					
						
							|  |  |  | char</code>. | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | Pointers in Go are pointers in C. A Go <code>struct</code> is the same as C | 
					
						
							|  |  |  | <code>struct</code> with the same fields and types. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  | The Go <code>string</code> type is currently defined as a two-element | 
					
						
							|  |  |  | structure (this is <b style="color: red;">subject to change</b>): | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | struct __go_string { | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  |   const unsigned char *__data; | 
					
						
							|  |  |  |   int __length; | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | You can't pass arrays between C and Go. However, a pointer to an | 
					
						
							|  |  |  | array in Go is equivalent to a C pointer to the | 
					
						
							|  |  |  | equivalent of the element type. | 
					
						
							|  |  |  | For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>, | 
					
						
							|  |  |  | assuming that the C pointer does point to 10 elements. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | A slice in Go is a structure.  The current definition is | 
					
						
							|  |  |  | (this is <b style="color: red;">subject to change</b>): | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | struct __go_slice { | 
					
						
							|  |  |  |   void *__values; | 
					
						
							|  |  |  |   int __count; | 
					
						
							|  |  |  |   int __capacity; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | The type of a Go function is a pointer to a struct (this is | 
					
						
							|  |  |  | <b style="color: red;">subject to change</b>).  The first field in the | 
					
						
							|  |  |  | struct points to the code of the function, which will be equivalent to | 
					
						
							|  |  |  | a pointer to a C function whose parameter types are equivalent, with | 
					
						
							|  |  |  | an additional trailing parameter.  The trailing parameter is the | 
					
						
							|  |  |  | closure, and the argument to pass is a pointer to the Go function | 
					
						
							|  |  |  | struct. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When a Go function returns more than one value, the C function returns | 
					
						
							|  |  |  | a struct.  For example, these functions are roughly equivalent: | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							| 
									
										
										
										
											2011-01-19 23:07:38 -05:00
										 |  |  | func GoFunction(int) (int, float64) | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | struct { int i; float64 f; } CFunction(int, void*) | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Go <code>interface</code>, <code>channel</code>, and <code>map</code> | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  | types have no corresponding C type (<code>interface</code> is a | 
					
						
							|  |  |  | two-element struct and <code>channel</code> and <code>map</code> are | 
					
						
							|  |  |  | pointers to structs in C, but the structs are deliberately undocumented). C | 
					
						
							|  |  |  | <code>enum</code> types correspond to some integer type, but precisely | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | which one is difficult to predict in general; use a cast. C <code>union</code> | 
					
						
							|  |  |  | types have no corresponding Go type. C <code>struct</code> types containing | 
					
						
							|  |  |  | bitfields have no corresponding Go type. C++ <code>class</code> types have | 
					
						
							|  |  |  | no corresponding Go type. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Memory allocation is completely different between C and Go, as Go uses | 
					
						
							|  |  |  | garbage collection. The exact guidelines in this area are undetermined, | 
					
						
							|  |  |  | but it is likely that it will be permitted to pass a pointer to allocated | 
					
						
							|  |  |  | memory from C to Go. The responsibility of eventually freeing the pointer | 
					
						
							|  |  |  | will remain with C side, and of course if the C side frees the pointer | 
					
						
							|  |  |  | while the Go side still has a copy the program will fail. When passing a | 
					
						
							|  |  |  | pointer from Go to C, the Go function must retain a visible copy of it in | 
					
						
							|  |  |  | some Go variable. Otherwise the Go garbage collector may delete the | 
					
						
							|  |  |  | pointer while the C function is still using it. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <h3 id="Function_names">Function names</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | Go code can call C functions directly using a Go extension implemented | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | in gccgo: a function declaration may be preceded by | 
					
						
							|  |  |  | <code>//extern NAME</code>.  For example, here is how the C function | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <code>open</code> can be declared in Go: | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | //extern open | 
					
						
							|  |  |  | func c_open(name *byte, mode int, perm int) int | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-19 08:26:36 +11:00
										 |  |  | The C function naturally expects a NUL-terminated string, which in | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | Go is equivalent to a pointer to an array (not a slice!) of | 
					
						
							|  |  |  | <code>byte</code> with a terminating zero byte. So a sample call | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | from Go would look like (after importing the <code>syscall</code> package): | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							|  |  |  | var name = [4]byte{'f', 'o', 'o', 0}; | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | i := c_open(&name[0], syscall.O_RDONLY, 0); | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | (this serves as an example only, to open a file in Go please use Go's | 
					
						
							|  |  |  | <code>os.Open</code> function instead). | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 14:49:15 -07:00
										 |  |  | <p> | 
					
						
							|  |  |  | Note that if the C function can block, such as in a call | 
					
						
							|  |  |  | to <code>read</code>, calling the C function may block the Go program. | 
					
						
							|  |  |  | Unless you have a clear understanding of what you are doing, all calls | 
					
						
							|  |  |  | between C and Go should be implemented through cgo or SWIG, as for | 
					
						
							|  |  |  | the <code>gc</code> compiler. | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | <p> | 
					
						
							|  |  |  | The name of Go functions accessed from C is subject to change. At present | 
					
						
							|  |  |  | the name of a Go function that does not have a receiver is | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  | <code>prefix.package.Functionname</code>. The prefix is set by | 
					
						
							|  |  |  | the <code>-fgo-prefix</code> option used when the package is compiled; | 
					
						
							| 
									
										
										
										
											2012-02-06 13:34:35 -05:00
										 |  |  | if the option is not used, the default is <code>go</code>. | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  | To call the function from C you must set the name using | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | a GCC extension. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <pre> | 
					
						
							| 
									
										
										
										
											2010-08-23 17:50:30 -07:00
										 |  |  | extern int go_function(int) __asm__ ("myprefix.mypackage.Function"); | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | </pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h3 id="Automatic_generation_of_Go_declarations_from_C_source_code"> | 
					
						
							|  |  |  | Automatic generation of Go declarations from C source code</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | The Go version of GCC supports automatically generating | 
					
						
							|  |  |  | Go declarations from C code. The facility is rather awkward, and most | 
					
						
							|  |  |  | users should use the <a href="/cmd/cgo">cgo</a> program with | 
					
						
							|  |  |  | the <code>-gccgo</code> option instead. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | Compile your C code as usual, and add the option | 
					
						
							|  |  |  | <code>-fdump-go-spec=<var>FILENAME</var></code>.  This will create the | 
					
						
							|  |  |  | file <code><var>FILENAME</var></code> as a side effect of the | 
					
						
							|  |  |  | compilation.  This file will contain Go declarations for the types, | 
					
						
							|  |  |  | variables and functions declared in the C code.  C types that can not | 
					
						
							|  |  |  | be represented in Go will be recorded as comments in the Go code.  The | 
					
						
							|  |  |  | generated file will not have a <code>package</code> declaration, but | 
					
						
							|  |  |  | can otherwise be compiled directly by gccgo. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2009-10-14 15:49:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p> | 
					
						
							|  |  |  | This procedure is full of unstated caveats and restrictions and we make no | 
					
						
							|  |  |  | guarantee that it will not change in the future. It is more useful as a | 
					
						
							|  |  |  | starting point for real Go code than as a regular procedure. | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | </p> | 
					
						
							| 
									
										
										
										
											2010-07-20 00:06:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-30 09:58:27 +10:00
										 |  |  | <h2 id="RTEMS_Port">RTEMS Port</h2> | 
					
						
							| 
									
										
										
										
											2010-07-20 00:06:20 -07:00
										 |  |  | <p> | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | The gccgo compiler has been ported to <a href="http://www.rtems.com/"> | 
					
						
							| 
									
										
										
										
											2010-07-20 00:06:20 -07:00
										 |  |  | <code>RTEMS</code></a>. <code>RTEMS</code> is a real-time executive | 
					
						
							|  |  |  | that provides a high performance environment for embedded applications | 
					
						
							| 
									
										
										
										
											2012-03-20 14:16:34 -07:00
										 |  |  | on a range of processors and embedded hardware. The current gccgo | 
					
						
							| 
									
										
										
										
											2010-07-20 00:06:20 -07:00
										 |  |  | port is for x86. The goal is to extend the port to most of the | 
					
						
							|  |  |  | <a href="http://www.rtems.org/wiki/index.php/SupportedCPUs"> | 
					
						
							|  |  |  | architectures supported by <code>RTEMS</code></a>. For more information on the port, | 
					
						
							|  |  |  | as well as instructions on how to install it, please see this | 
					
						
							| 
									
										
										
										
											2014-04-28 06:27:22 -07:00
										 |  |  | <a href="http://www.rtems.org/wiki/index.php/GCCGoRTEMS"><code>RTEMS</code> Wiki page</a>. |