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