gccgo_install.html 17 KB

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