Using the C language code instance in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Using the C language code instance in Golang

Submission: Junjie font: [Increase listening decrease] Type: Reprint time: 2014-10-27 Listen, I want to comment.

This article mainly introduces the use of C language code examples in Golang, this article first gives a Hello World example, Golang reference C example, and summarizes some of the areas to note, the need for friends can refer to the following

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:

Copy the Code code as follows:


Package Main
Hear
/*
#include <stdio.h>
Hear
void Sayhi () {
Listen and listen to printf ("Hi");
}
*/
Import "C"
Hear
Func Main () {
Listen and listen to C.sayhi ()
}


Execute the program:

Copy the Code code as follows:


Go Run main.go


The program executes and outputs hi (more examples can be seen $GOROOT/misc/cgo).


Preparations under Windows

If you want to use CGO on Windows, then you need to install the GCC compiler, here I use MINGW-W64.

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:

1. Compiler and linker flags
2.C Code

We can set compiler and linker flags by #cgo directives, for example:

Copy the Code code as follows:


#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 1.CPPFLAGS, CFLAGS instruction is used to compile the C file in the current package (any. C,. S,. S file)
The 2.CPPFLAGS, cxxflags instruction is used to compile C + + files in the current package (any. cpp,. cc,. cxx files)
The 3.LDFLAGS directive is used to specify linker flags
The 4.pkg-config directive is used to obtain compiler and linker flags through the Pkg-config tool (for example: #cgo pkg-config:png Cairo)

Golang Reference C

Points to note on the structure:

1.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.
2. A struct's bit field, non-aligned data, and so on cannot be ignored when represented in Golang
A domain of type C cannot be used in a 3.Golang struct

The standard C numeric type corresponds to:

1.c.char
2.c.schar (signed Char)
3.c.uchar (unsigned char)
4.c.short
5.c.ushort (unsigned short)
6.c.int
7.c.uint (unsigned int)
8.c.long
9.c.ulong (unsigned long)
10.c.longlong (Long Long)
11.c.ulonglong (unsigned long Long)
12.c.float
13.c.double

Any C function (including void functions) can return a return value and C's errno variable (as an error):

Copy the Code code as follows:


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:


Copy the Code code as follows:


Golang string to C string
The string for C is allocated using malloc, so the caller of this function
Need to call C.free to free memory
Func c.cstring (String) *c.char
Hear
Convert C string to Golang string
Func c.gostring (*c.char) string
Hear
Converts a certain length of C string to a Golang string
Func c.gostringn (*c.char, C.int) string
Hear
Convert a chunk of C memory into a byte array of Golang
Func c.gobytes (unsafe. Pointer, c.int) []byte


Other points to note:

The void* in the 1.C language corresponds to unsafe. Pointer
The structure, Union, enumeration type (not the variable) in the 2.C language requires struct_, union_, enum_ prefix access in the Golang. Because the data type is not federated in Golang, the union of C is represented as a byte array in Golang
3. Those types that are equivalent to the C language are not exportable


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.