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