gccgo.texi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. \input texinfo @c -*-texinfo-*-
  2. @setfilename gccgo.info
  3. @settitle The GNU Go Compiler
  4. @c Merge the standard indexes into a single one.
  5. @syncodeindex fn cp
  6. @syncodeindex vr cp
  7. @syncodeindex ky cp
  8. @syncodeindex pg cp
  9. @syncodeindex tp cp
  10. @include gcc-common.texi
  11. @c Copyright years for this manual.
  12. @set copyrights-go 2010-2015
  13. @copying
  14. @c man begin COPYRIGHT
  15. Copyright @copyright{} @value{copyrights-go} Free Software Foundation, Inc.
  16. Permission is granted to copy, distribute and/or modify this document
  17. under the terms of the GNU Free Documentation License, Version 1.3 or
  18. any later version published by the Free Software Foundation; with no
  19. Invariant Sections, the Front-Cover Texts being (a) (see below), and
  20. with the Back-Cover Texts being (b) (see below).
  21. A copy of the license is included in the
  22. @c man end
  23. section entitled ``GNU Free Documentation License''.
  24. @ignore
  25. @c man begin COPYRIGHT
  26. man page gfdl(7).
  27. @c man end
  28. @end ignore
  29. @c man begin COPYRIGHT
  30. (a) The FSF's Front-Cover Text is:
  31. A GNU Manual
  32. (b) The FSF's Back-Cover Text is:
  33. You have freedom to copy and modify this GNU Manual, like GNU
  34. software. Copies published by the Free Software Foundation raise
  35. funds for GNU development.
  36. @c man end
  37. @end copying
  38. @ifinfo
  39. @format
  40. @dircategory Software development
  41. @direntry
  42. * Gccgo: (gccgo). A GCC-based compiler for the Go language
  43. @end direntry
  44. @end format
  45. @insertcopying
  46. @end ifinfo
  47. @titlepage
  48. @title The GNU Go Compiler
  49. @versionsubtitle
  50. @author Ian Lance Taylor
  51. @page
  52. @vskip 0pt plus 1filll
  53. Published by the Free Software Foundation @*
  54. 51 Franklin Street, Fifth Floor@*
  55. Boston, MA 02110-1301, USA@*
  56. @sp 1
  57. @insertcopying
  58. @end titlepage
  59. @contents
  60. @page
  61. @node Top
  62. @top Introduction
  63. This manual describes how to use @command{gccgo}, the GNU compiler for
  64. the Go programming language. This manual is specifically about
  65. @command{gccgo}. For more information about the Go programming
  66. language in general, including language specifications and standard
  67. package documentation, see @uref{http://golang.org/}.
  68. @menu
  69. * Copying:: The GNU General Public License.
  70. * GNU Free Documentation License::
  71. How you can share and copy this manual.
  72. * Invoking gccgo:: How to run gccgo.
  73. * Import and Export:: Importing and exporting package data.
  74. * C Interoperability:: Calling C from Go and vice-versa.
  75. * Index:: Index.
  76. @end menu
  77. @include gpl_v3.texi
  78. @include fdl.texi
  79. @node Invoking gccgo
  80. @chapter Invoking gccgo
  81. @c man title gccgo A GCC-based compiler for the Go language
  82. @ignore
  83. @c man begin SYNOPSIS gccgo
  84. gccgo [@option{-c}|@option{-S}]
  85. [@option{-g}] [@option{-pg}] [@option{-O}@var{level}]
  86. [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
  87. [@option{-o} @var{outfile}] @var{infile}@dots{}
  88. Only the most useful options are listed here; see below for the
  89. remainder.
  90. @c man end
  91. @c man begin SEEALSO
  92. gpl(7), gfdl(7), fsf-funding(7), gcc(1)
  93. and the Info entries for @file{gccgo} and @file{gcc}.
  94. @c man end
  95. @end ignore
  96. @c man begin DESCRIPTION gccgo
  97. The @command{gccgo} command is a frontend to @command{gcc} and
  98. supports many of the same options. @xref{Option Summary, , Option
  99. Summary, gcc, Using the GNU Compiler Collection (GCC)}. This manual
  100. only documents the options specific to @command{gccgo}.
  101. The @command{gccgo} command may be used to compile Go source code into
  102. an object file, link a collection of object files together, or do both
  103. in sequence.
  104. Go source code is compiled as packages. A package consists of one or
  105. more Go source files. All the files in a single package must be
  106. compiled together, by passing all the files as arguments to
  107. @command{gccgo}. A single invocation of @command{gccgo} may only
  108. compile a single package.
  109. One Go package may @code{import} a different Go package. The imported
  110. package must have already been compiled; @command{gccgo} will read
  111. the import data directly from the compiled package. When this package
  112. is later linked, the compiled form of the package must be included in
  113. the link command.
  114. Go programs must generally be compiled with debugging information, and
  115. @option{-g1} is the default as described below. Stripping a Go
  116. program will generally cause it to misbehave or fail.
  117. @c man end
  118. @c man begin OPTIONS gccgo
  119. @table @gcctabopt
  120. @item -I@var{dir}
  121. @cindex @option{-I}
  122. Specify a directory to use when searching for an import package at
  123. compile time.
  124. @item -L@var{dir}
  125. @cindex @option{-L}
  126. When linking, specify a library search directory, as with
  127. @command{gcc}.
  128. @item -fgo-pkgpath=@var{string}
  129. @cindex @option{-fgo-pkgpath}
  130. Set the package path to use. This sets the value returned by the
  131. PkgPath method of reflect.Type objects. It is also used for the names
  132. of globally visible symbols. The argument to this option should
  133. normally be the string that will be used to import this package after
  134. it has been installed; in other words, a pathname within the
  135. directories specified by the @option{-I} option.
  136. @item -fgo-prefix=@var{string}
  137. @cindex @option{-fgo-prefix}
  138. An alternative to @option{-fgo-pkgpath}. The argument will be
  139. combined with the package name from the source file to produce the
  140. package path. If @option{-fgo-pkgpath} is used, @option{-fgo-prefix}
  141. will be ignored.
  142. Go permits a single program to include more than one package with the
  143. same name in the @code{package} clause in the source file, though
  144. obviously the two packages must be imported using different pathnames.
  145. In order for this to work with @command{gccgo}, either
  146. @option{-fgo-pkgpath} or @option{-fgo-prefix} must be specified when
  147. compiling a package.
  148. Using either @option{-fgo-pkgpath} or @option{-fgo-prefix} disables
  149. the special treatment of the @code{main} package and permits that
  150. package to be imported like any other.
  151. @item -fgo-relative-import-path=@var{dir}
  152. @cindex @option{-fgo-relative-import-path}
  153. A relative import is an import that starts with @file{./} or
  154. @file{../}. If this option is used, @command{gccgo} will use
  155. @var{dir} as a prefix for the relative import when searching for it.
  156. @item -frequire-return-statement
  157. @itemx -fno-require-return-statement
  158. @cindex @option{-frequire-return-statement}
  159. @cindex @option{-fno-require-return-statement}
  160. By default @command{gccgo} will warn about functions which have one or
  161. more return parameters but lack an explicit @code{return} statement.
  162. This warning may be disabled using
  163. @option{-fno-require-return-statement}.
  164. @item -fgo-check-divide-zero
  165. @cindex @option{-fgo-check-divide-zero}
  166. @cindex @option{-fno-go-check-divide-zero}
  167. Add explicit checks for division by zero. In Go a division (or
  168. modulos) by zero causes a panic. On Unix systems this is detected in
  169. the runtime by catching the @code{SIGFPE} signal. Some processors,
  170. such as PowerPC, do not generate a SIGFPE on division by zero. Some
  171. runtimes do not generate a signal that can be caught. On those
  172. systems, this option may be used. Or the checks may be removed via
  173. @option{-fno-go-check-divide-zero}. This option is currently on by
  174. default, but in the future may be off by default on systems that do
  175. not require it.
  176. @item -fgo-check-divide-overflow
  177. @cindex @option{-fgo-check-divide-overflow}
  178. @cindex @option{-fno-go-check-divide-overflow}
  179. Add explicit checks for division overflow. For example, division
  180. overflow occurs when computing @code{INT_MIN / -1}. In Go this should
  181. be wrapped, to produce @code{INT_MIN}. Some processors, such as x86,
  182. generate a trap on division overflow. On those systems, this option
  183. may be used. Or the checks may be removed via
  184. @option{-fno-go-check-divide-overflow}. This option is currently on
  185. by default, but in the future may be off by default on systems that do
  186. not require it.
  187. @item -g
  188. @cindex @option{-g for gccgo}
  189. This is the standard @command{gcc} option (@pxref{Debugging Options, ,
  190. Debugging Options, gcc, Using the GNU Compiler Collection (GCC)}). It
  191. is mentioned here because by default @command{gccgo} turns on
  192. debugging information generation with the equivalent of the standard
  193. option @option{-g1}. This is because Go programs require debugging
  194. information to be available in order to get backtrace information. An
  195. explicit @option{-g0} may be used to disable the generation of
  196. debugging information, in which case certain standard library
  197. functions, such as @code{runtime.Callers}, will not operate correctly.
  198. @end table
  199. @c man end
  200. @node Import and Export
  201. @chapter Import and Export
  202. When @command{gccgo} compiles a package which exports anything, the
  203. export information will be stored directly in the object file. When a
  204. package is imported, @command{gccgo} must be able to find the file.
  205. @cindex @file{.gox}
  206. When Go code imports the package @file{@var{gopackage}}, @command{gccgo}
  207. will look for the import data using the following filenames, using the
  208. first one that it finds.
  209. @table @file
  210. @item @var{gopackage}.gox
  211. @item lib@var{gopackage}.so
  212. @item lib@var{gopackage}.a
  213. @item @var{gopackage}.o
  214. @end table
  215. The compiler will search for these files in the directories named by
  216. any @option{-I} options, in order in which the directories appear on
  217. the command line. The compiler will then search several standard
  218. system directories. Finally the compiler will search the current
  219. directory (to search the current directory earlier, use @samp{-I.}).
  220. The compiler will extract the export information directly from the
  221. compiled object file. The file @file{@var{gopackage}.gox} will
  222. typically contain nothing but export data. This can be generated from
  223. @file{@var{gopackage}.o} via
  224. @smallexample
  225. objcopy -j .go_export @var{gopackage}.o @var{gopackage}.gox
  226. @end smallexample
  227. For example, it may be desirable to extract the export information
  228. from several different packages into their independent
  229. @file{@var{gopackage}.gox} files, and then to combine the different
  230. package object files together into a single shared library or archive.
  231. At link time you must explicitly tell @command{gccgo} which files to
  232. link together into the executable, as is usual with @command{gcc}.
  233. This is different from the behaviour of other Go compilers.
  234. @node C Interoperability
  235. @chapter C Interoperability
  236. When using @command{gccgo} there is limited interoperability with C,
  237. or with C++ code compiled using @code{extern "C"}.
  238. This information is provided largely for documentation purposes. For
  239. ordinary use it is best to build programs with the go tool and then
  240. use @code{import "C"}, as described at
  241. @url{http://golang.org/cmd/cgo}.
  242. @menu
  243. * C Type Interoperability:: How C and Go types match up.
  244. * Function Names:: How Go functions are named.
  245. @end menu
  246. @node C Type Interoperability
  247. @section C Type Interoperability
  248. Basic types map directly: an @code{int} in Go is an @code{int} in C,
  249. etc. Go @code{byte} is equivalent to C @code{unsigned char}.
  250. Pointers in Go are pointers in C. A Go @code{struct} is the same as C
  251. @code{struct} with the same field names and types.
  252. @cindex @code{string} in C
  253. The Go @code{string} type is currently defined as a two-element
  254. structure:
  255. @smallexample
  256. struct __go_string @{
  257. const unsigned char *__data;
  258. int __length;
  259. @};
  260. @end smallexample
  261. You can't pass arrays between C and Go. However, a pointer to an
  262. array in Go is equivalent to a C pointer to the equivalent of the
  263. element type. For example, Go @code{*[10]int} is equivalent to C
  264. @code{int*}, assuming that the C pointer does point to 10 elements.
  265. @cindex @code{slice} in C
  266. A slice in Go is a structure. The current definition is:
  267. @smallexample
  268. struct __go_slice @{
  269. void *__values;
  270. int __count;
  271. int __capacity;
  272. @};
  273. @end smallexample
  274. The type of a Go function with no receiver is equivalent to a C
  275. function whose parameter types are equivalent. When a Go function
  276. returns more than one value, the C function returns a struct. For
  277. example, these functions have equivalent types:
  278. @smallexample
  279. func GoFunction(int) (int, float)
  280. struct @{ int i; float f; @} CFunction(int)
  281. @end smallexample
  282. A pointer to a Go function is equivalent to a pointer to a C function
  283. when the functions have equivalent types.
  284. Go @code{interface}, @code{channel}, and @code{map} types have no
  285. corresponding C type (@code{interface} is a two-element struct and
  286. @code{channel} and @code{map} are pointers to structs in C, but the
  287. structs are deliberately undocumented). C @code{enum} types
  288. correspond to some integer type, but precisely which one is difficult
  289. to predict in general; use a cast. C @code{union} types have no
  290. corresponding Go type. C @code{struct} types containing bitfields
  291. have no corresponding Go type. C++ @code{class} types have no
  292. corresponding Go type.
  293. Memory allocation is completely different between C and Go, as Go uses
  294. garbage collection. The exact guidelines in this area are
  295. undetermined, but it is likely that it will be permitted to pass a
  296. pointer to allocated memory from C to Go. The responsibility of
  297. eventually freeing the pointer will remain with C side, and of course
  298. if the C side frees the pointer while the Go side still has a copy the
  299. program will fail. When passing a pointer from Go to C, the Go
  300. function must retain a visible copy of it in some Go variable.
  301. Otherwise the Go garbage collector may delete the pointer while the C
  302. function is still using it.
  303. @node Function Names
  304. @section Function Names
  305. @cindex @code{extern}
  306. @cindex external names
  307. Go code can call C functions directly using a Go extension implemented
  308. in @command{gccgo}: a function declaration may be preceded by a
  309. comment giving the external name. The comment must be at the
  310. beginning of the line and must start with @code{//extern}. This must
  311. be followed by a space and then the external name of the function.
  312. The function declaration must be on the line immediately after the
  313. comment. For example, here is how the C function @code{open} can be
  314. declared in Go:
  315. @smallexample
  316. //extern open
  317. func c_open(name *byte, mode int, perm int) int
  318. @end smallexample
  319. The C function naturally expects a nul terminated string, which in Go
  320. is equivalent to a pointer to an array (not a slice!) of @code{byte}
  321. with a terminating zero byte. So a sample call from Go would look
  322. like (after importing the @code{os} package):
  323. @smallexample
  324. var name = [4]byte@{'f', 'o', 'o', 0@};
  325. i := c_open(&name[0], os.O_RDONLY, 0);
  326. @end smallexample
  327. Note that this serves as an example only. To open a file in Go please
  328. use Go's @code{os.Open} function instead.
  329. The name of Go functions accessed from C is subject to change. At
  330. present the name of a Go function that does not have a receiver is
  331. @code{prefix.package.Functionname}. The prefix is set by the
  332. @option{-fgo-prefix} option used when the package is compiled; if the
  333. option is not used, the default is simply @code{go}. To call the
  334. function from C you must set the name using the @command{gcc}
  335. @code{__asm__} extension.
  336. @smallexample
  337. extern int go_function(int) __asm__ ("myprefix.mypackage.Function");
  338. @end smallexample
  339. @node Index
  340. @unnumbered Index
  341. @printindex cp
  342. @bye