LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 /* | 5 /* |
6 | 6 |
7 Cgo enables the creation of Go packages that call C code. | 7 Cgo enables the creation of Go packages that call C code. |
8 | 8 |
9 Using cgo with the go command | 9 Using cgo with the go command |
10 | 10 |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 C source. | 262 C source. |
263 | 263 |
264 Cgo first invokes gcc -E -dM on the preamble, in order to find out | 264 Cgo first invokes gcc -E -dM on the preamble, in order to find out |
265 about simple #defines for constants and the like. These are recorded | 265 about simple #defines for constants and the like. These are recorded |
266 for later use. | 266 for later use. |
267 | 267 |
268 Next, cgo needs to identify the kinds for each identifier. For the | 268 Next, cgo needs to identify the kinds for each identifier. For the |
269 identifiers C.foo and C.bar, cgo generates this C program: | 269 identifiers C.foo and C.bar, cgo generates this C program: |
270 | 270 |
271 <preamble> | 271 <preamble> |
272 » void __cgo__f__(void) { | 272 » #line 1 "not-declared" |
273 » #line 1 "cgo-test" | 273 » void __cgo_f_xxx_1(void) { typeof(foo) *__cgo_undefined__; } |
274 » » foo; | 274 » #line 1 "not-type" |
275 » » enum { _cgo_enum_0 = foo }; | 275 » void __cgo_f_xxx_2(void) { foo *__cgo_undefined__; } |
276 » » bar; | 276 » #line 1 "not-const" |
277 » » enum { _cgo_enum_1 = bar }; | 277 » void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (foo)*1 }; } |
278 » } | 278 » #line 2 "not-declared" |
279 | 279 » void __cgo_f_xxx_1(void) { typeof(bar) *__cgo_undefined__; } |
280 This program will not compile, but cgo can look at the error messages | 280 » #line 2 "not-type" |
281 to infer the kind of each identifier. The line number given in the | 281 » void __cgo_f_xxx_2(void) { bar *__cgo_undefined__; } |
282 error tells cgo which identifier is involved. | 282 » #line 2 "not-const" |
283 | 283 » void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (bar)*1 }; } |
284 An error like "unexpected type name" or "useless type name in empty | 284 |
285 declaration" or "declaration does not declare anything" tells cgo that | 285 This program will not compile, but cgo can use the presence or absence |
286 the identifier is a type. | 286 of an error message on a given line to deduce the information it |
287 | 287 needs. The program is syntactically valid regardless of whether each |
288 An error like "statement with no effect" or "expression result unused" | 288 name is a type or an ordinary identifier, so there will be no syntax |
289 tells cgo that the identifier is not a type, but not whether it is a | 289 errors that might stop parsing early. |
290 constant, function, or global variable. | 290 |
291 | 291 An error on not-declared:1 indicates that foo is undeclared. |
292 An error like "not an integer constant" tells cgo that the identifier | 292 An error on not-type:1 indicates that foo is not a type (if declared at all, it
is an identifier). |
293 is not a constant. If it is also not a type, it must be a function or | 293 An error on not-const:1 indicates that foo is not an integer constant. |
294 global variable. For now, those can be treated the same. | 294 |
| 295 The line number specifies the name involved. In the example, 1 is foo and 2 is b
ar. |
295 | 296 |
296 Next, cgo must learn the details of each type, variable, function, or | 297 Next, cgo must learn the details of each type, variable, function, or |
297 constant. It can do this by reading object files. If cgo has decided | 298 constant. It can do this by reading object files. If cgo has decided |
298 that t1 is a type, v2 and v3 are variables or functions, and c4, c5, | 299 that t1 is a type, v2 and v3 are variables or functions, and c4, c5, |
299 and c6 are constants, it generates: | 300 and c6 are constants, it generates: |
300 | 301 |
301 <preamble> | 302 <preamble> |
302 typeof(t1) *__cgo__1; | 303 typeof(t1) *__cgo__1; |
303 typeof(v2) *__cgo__2; | 304 typeof(v2) *__cgo__2; |
304 typeof(v3) *__cgo__3; | 305 typeof(v3) *__cgo__3; |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 be overridden using command line flags: 6l -extld=clang | 711 be overridden using command line flags: 6l -extld=clang |
711 -extldflags='-ggdb -O3'. If any package in a build includes a .cc or | 712 -extldflags='-ggdb -O3'. If any package in a build includes a .cc or |
712 other file compiled by the C++ compiler, the go tool will use the | 713 other file compiled by the C++ compiler, the go tool will use the |
713 -extld option to set the host linker to the C++ compiler. | 714 -extld option to set the host linker to the C++ compiler. |
714 | 715 |
715 These defaults mean that Go-aware build systems can ignore the linking | 716 These defaults mean that Go-aware build systems can ignore the linking |
716 changes and keep running plain '6l' and get reasonable results, but | 717 changes and keep running plain '6l' and get reasonable results, but |
717 they can also control the linking details if desired. | 718 they can also control the linking details if desired. |
718 | 719 |
719 */ | 720 */ |
LEFT | RIGHT |