github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/doc/gccgo_install.html (about)

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