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