This is a creation in Article, where the information may have evolved or changed. List of reference documents:
http://golang.org/cmd/cgo/
CGO makes it possible to use C code in Golang.
Hello World
To have a more intuitive understanding, let's look at a simple example of creating a file Main.go:
Package Main/* #include <stdio.h> void Sayhi () { printf ("Hi");} */import "C" Func Main () { c.sayhi ()}
Execute the program:
Go Run main.go
The program executes and outputs hi (more examples can be seen $GOROOT/misc/cgo). We can use the command:
Go build-x main.go
To display the commands at build time.
Preparations under Windows
If you want to use CGO on Windows, then you need to install the GCC compiler, here I use MINGW-W64, can be downloaded here, here, you can also use the TDM-GCC.
Set compilation and link flags
We imported a pseudo package (Pseudo-package) using the import "C", which we used to use C code. Before import "C", comments that follow the import "C" can include:
Compiler and linker flags
C Code
We can set compiler and linker flags by #cgo directives, for example:
#cgo CFLAGS:-dpng_debug=1//#cgo AMD64 386 CFLAGS:-dx86=1//#cgo ldflags:-lpng//#include <png.h>import "C"
Incidentally, these directives can contain build constraints (build constraint), as detailed in the following: http://golang.org/pkg/go/build/#hdr-build_constraints.
The commonly used #cgo directives are:
The cppflags, CFLAGS instructions are used to compile the C file in the current package (any. C,. S,. S file)
Cppflags, cxxflags directives are used to compile C + + files in the current package (any. cpp,. cc,. cxx files)
Ldflags directives for specifying linker flags
The pkg-config directive is used to obtain compiler and linker flags through the Pkg-config tool (for example: #cgo pkg-config:png Cairo)
When the Golang tool discovers that there is a Golang source file that uses the import "C", it will look for non-Golang source files in the current directory and compile them as part of this Golang package:
File test.c
#include <stdio.h> void Sayhi () { printf ("Hi");} File Test.gopackage main/*extern void Sayhi () */import "C" Func Main () { c.sayhi ()}
To build your work:
Go Build
It is important to note that using go build test.go will not compile the test.c file.
Golang Reference C
Points to note on the structure:
C struct domain name if the keyword is Golang, the access needs to precede the domain name with _. For example, there is a struct variable x in C, and there is a field type in the struct that corresponds to this variable, so you need to access the Type field through X._type in Golang.
A struct's bit field, unaligned data, and so on cannot be ignored when represented in Golang
Golang a domain of type C cannot be used in a struct
The standard C numeric type corresponds to:
C.charc.schar (signed char) C.uchar (unsigned char) c.shortc.ushort (unsigned short) c.intc.uint (unsigned int) C.longc.ulong (unsigned long) c.longlong (Long Long) c.ulonglong (unsigned long long) c.floatc.double
Any C function (including void functions) can return a return value and C's errno variable (as an error):
N, Err: = C.sqrt ( -1) _, Err: = C.voidfunc ()
A direct call to a C function pointer is not currently supported.
There are special functions that can be used to convert between C and Golang types (by means of data copies), pseudo-defined as follows:
Golang string to C string//C string is allocated using malloc, so the caller of this function//needs to call C.free to free memory func c.cstring (String) *c.char//Convert C string to go Lang string func c.gostring (*c.char) string//Convert a certain length of C string to Golang string func c.gostringn (*c.char, C.int) string//Convert a piece of C memory area To the Golang byte array, go to func c.gobytes (unsafe. Pointer, c.int) []byte
Other points to note:
The void* in C language corresponds to unsafe. Pointer
The structure, Union, enumeration type (and not the variable) in C language requires struct_, union_, enum_ prefix access in Golang. Because the data type is not federated in Golang, the union of C is represented as a byte array in Golang
Those types that are equivalent to the C language are not exportable.
C Reference Golang
The functions of Golang can be exported to C using:
Export Myfunctionfunc MyFunction (arg1, arg2 int, arg3 string) Int64 {...} Export Myfunction2func MyFunction2 (arg1, arg2 int, arg3 string) (Int64, *c.char) {...}
The corresponding C code is (generated in _cgo_export.h):
extern int64 MyFunction (int arg1, int arg2, gostring arg3), extern struct Myfunction2_return MyFunction2 (int arg1, int arg2 , gostring Arg3);
There are a few points to note:
Multiple return values of the Golang function are mapped to a C struct body
Use//export to enable the Golang function to be referenced in C. After//export is used, the definition of C cannot be included in this Golang source file (but can be declared)
References:
Http://name5566.com/4934.html?utm_source=tuicool