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