2009-11-04 15:17:36 -08:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Cgo enables the creation of Go packages that call C code.
|
|
|
|
|
|
2012-02-04 07:03:20 +11:00
|
|
|
Usage: go tool cgo [compiler options] file.go
|
2009-11-04 15:17:36 -08:00
|
|
|
|
|
|
|
|
The compiler options are passed through uninterpreted when
|
|
|
|
|
invoking gcc to compile the C parts of the package.
|
|
|
|
|
|
|
|
|
|
The input file.go is a syntactically valid Go source file that imports
|
|
|
|
|
the pseudo-package "C" and then refers to types such as C.size_t,
|
|
|
|
|
variables such as C.stdout, or functions such as C.putchar.
|
|
|
|
|
|
|
|
|
|
If the import of "C" is immediately preceded by a comment, that
|
|
|
|
|
comment is used as a header when compiling the C parts of
|
|
|
|
|
the package. For example:
|
|
|
|
|
|
|
|
|
|
// #include <stdio.h>
|
|
|
|
|
// #include <errno.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
2011-02-01 08:44:18 -05:00
|
|
|
CFLAGS and LDFLAGS may be defined with pseudo #cgo directives
|
|
|
|
|
within these comments to tweak the behavior of gcc. Values defined
|
2011-02-03 13:51:47 -05:00
|
|
|
in multiple directives are concatenated together. Options prefixed
|
|
|
|
|
by $GOOS, $GOARCH, or $GOOS/$GOARCH are only defined in matching
|
|
|
|
|
systems. For example:
|
2011-02-01 08:44:18 -05:00
|
|
|
|
|
|
|
|
// #cgo CFLAGS: -DPNG_DEBUG=1
|
2011-02-03 13:51:47 -05:00
|
|
|
// #cgo linux CFLAGS: -DLINUX=1
|
2011-02-01 08:44:18 -05:00
|
|
|
// #cgo LDFLAGS: -lpng
|
|
|
|
|
// #include <png.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
2011-05-26 22:19:23 -03:00
|
|
|
Alternatively, CFLAGS and LDFLAGS may be obtained via the pkg-config
|
|
|
|
|
tool using a '#cgo pkg-config:' directive followed by the package names.
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
// #cgo pkg-config: png cairo
|
|
|
|
|
// #include <png.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
|
|
Within the Go file, C identifiers or field names that are keywords in Go
|
|
|
|
|
can be accessed by prefixing them with an underscore: if x points at a C
|
|
|
|
|
struct with a field named "type", x._type accesses the field.
|
2010-07-14 17:17:53 -07:00
|
|
|
|
2010-09-21 22:41:19 -04:00
|
|
|
The standard C numeric types are available under the names
|
|
|
|
|
C.char, C.schar (signed char), C.uchar (unsigned char),
|
|
|
|
|
C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
|
|
|
|
|
C.long, C.ulong (unsigned long), C.longlong (long long),
|
|
|
|
|
C.ulonglong (unsigned long long), C.float, C.double.
|
2011-07-28 12:39:50 -04:00
|
|
|
The C type void* is represented by Go's unsafe.Pointer.
|
2010-09-21 22:41:19 -04:00
|
|
|
|
2010-07-14 17:17:53 -07:00
|
|
|
To access a struct, union, or enum type directly, prefix it with
|
|
|
|
|
struct_, union_, or enum_, as in C.struct_stat.
|
|
|
|
|
|
|
|
|
|
Any C function that returns a value may be called in a multiple
|
|
|
|
|
assignment context to retrieve both the return value and the
|
2011-11-01 21:49:22 -04:00
|
|
|
C errno variable as an error. For example:
|
2010-07-14 17:17:53 -07:00
|
|
|
|
|
|
|
|
n, err := C.atoi("abc")
|
|
|
|
|
|
2010-09-21 22:41:19 -04:00
|
|
|
In C, a function argument written as a fixed size array
|
|
|
|
|
actually requires a pointer to the first element of the array.
|
|
|
|
|
C compilers are aware of this calling convention and adjust
|
|
|
|
|
the call accordingly, but Go cannot. In Go, you must pass
|
|
|
|
|
the pointer to the first element explicitly: C.f(&x[0]).
|
|
|
|
|
|
2011-07-28 12:39:50 -04:00
|
|
|
A few special functions convert between Go and C types
|
|
|
|
|
by making copies of the data. In pseudo-Go definitions:
|
|
|
|
|
|
2011-08-31 08:17:01 -04:00
|
|
|
// Go string to C string
|
|
|
|
|
// The C string is allocated in the C heap using malloc.
|
|
|
|
|
// It is the caller's responsibility to arrange for it to be
|
|
|
|
|
// freed, such as by calling C.free.
|
2011-07-28 12:39:50 -04:00
|
|
|
func C.CString(string) *C.char
|
|
|
|
|
|
|
|
|
|
// C string to Go string
|
|
|
|
|
func C.GoString(*C.char) string
|
|
|
|
|
|
|
|
|
|
// C string, length to Go string
|
|
|
|
|
func C.GoStringN(*C.char, C.int) string
|
|
|
|
|
|
|
|
|
|
// C pointer, length to Go []byte
|
|
|
|
|
func C.GoBytes(unsafe.Pointer, C.int) []byte
|
|
|
|
|
|
2011-11-18 18:24:59 -05:00
|
|
|
Go functions can be exported for use by C code in the following way:
|
|
|
|
|
|
|
|
|
|
//export MyFunction
|
|
|
|
|
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
|
|
|
|
|
|
|
|
|
|
//export MyFunction2
|
2011-12-06 08:16:39 -08:00
|
|
|
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
|
2011-11-18 18:24:59 -05:00
|
|
|
|
|
|
|
|
They will be available in the C code as:
|
|
|
|
|
|
|
|
|
|
extern int64 MyFunction(int arg1, int arg2, GoString arg3);
|
|
|
|
|
extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);
|
|
|
|
|
|
|
|
|
|
found in _cgo_export.h generated header. Functions with multiple
|
|
|
|
|
return values are mapped to functions returning a struct.
|
|
|
|
|
Not all Go types can be mapped to C types in a useful way.
|
|
|
|
|
|
2009-11-04 15:17:36 -08:00
|
|
|
Cgo transforms the input file into four output files: two Go source
|
|
|
|
|
files, a C file for 6c (or 8c or 5c), and a C file for gcc.
|
|
|
|
|
|
|
|
|
|
The standard package makefile rules in Make.pkg automate the
|
|
|
|
|
process of using cgo. See $GOROOT/misc/cgo/stdio and
|
|
|
|
|
$GOROOT/misc/cgo/gmp for examples.
|
|
|
|
|
|
|
|
|
|
Cgo does not yet work with gccgo.
|
2011-09-10 09:35:25 +10:00
|
|
|
|
|
|
|
|
See "C? Go? Cgo!" for an introduction to using cgo:
|
|
|
|
|
http://blog.golang.org/2011/03/c-go-cgo.html
|
2009-11-04 15:17:36 -08:00
|
|
|
*/
|
|
|
|
|
package documentation
|