Call C in Golang

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

call C in Golang

2010-04-18 13:21:28  |  Category: accumulation | Tags:| font size big medium small Subscribe

Golang Call C Two steps: 1 write a C wrapper, this is very simple, 2 to wrapper do compile, this step is a bit complicated, and involves a lot of intermediate files. There should be a way to simplify the process with automated tools.
First to show the C program. To focus the description on how to call, C's program is simple: prints.h #ifndef Prints_head void Prints (char* str); #endif
Prints.c #include "prints.h" #include <stdio.h>
void Prints (char* str) { printf ("%s\n", str); }
followed by a wrapper:prints.go of Golang on C. Package Prints
//#include "prints.h" //// some comment import "C"
func Prints (s string) { p: = c.cstring (s); c.prints (p); }
It is important to note that several lines are highlighted in red. During the compilation process, the go will be based on the Imp Ort a few lines before the "C" generates a C program and imports the symbols from this C program into module C, and finally by Imp Ort "C" is then imported into the Go program. If you need to invoke the API in other go programs, you will need to write a wrapper func by referring to the prints function in Prints.go (the Go module to be exported requires a first letter capitalization). References to the symbols in the C program need to be referenced by C, including some type definitions of C, such as those passed to the C API, which need to be defined by c.int, while strings are c.cstring.
With these files, you can compile a library that can be loaded in go. The following are the operating procedures under x86 Linux, and in the case of other environments, replace the appropriate compilation commands.
CGO Prints.goCompile wrapper, generate file: _cgo_defun.c: According to prints.go Red annotation, generate the C symbol to be called in go and function _cgo_gotypes.go:_cgo_defun.c symbol in go corresponding definition _cgo_. Oprints.cgo1.go: Prints.go generated by Go wrapper funcprints.cgo2.c: C prints.go func based on wrapper
8g-o _go_.8 prints.cgo1.go _cgo_gotypes.goCompile go wrapper related files, generate _go_.8
8c-fvw-i "/home/lizh/go/src/pkg/runtime/" _cgo_defun.cCompile the generic part of C wrapper, generate _cgo_defun.8
gopack GRC PRINTS.A _go_.8 _cgo_defun.8Package the above two compiled wrapper to generate the PRINTS.A
CP prints.a $GOROOT/pkg/linux_386/Put the generated PRINTS.A in the Go package directory
After that is the compilation of the C part: gcc-m32-fpic-o2-o prints.cgo2.o-c prints.cgo2.c gcc-m32-fpic-o2-o prints.o-c prints.c gcc-m32-o prints.so prints.o prints.cgo2.o-sharedBuild prints.so based on PRINTS.C and prints.cgo2.c, which is a dynamic library that can be introduced to go programs. by objdump viewing the prints.so symbol, you can find: Prints: The C API symbol _cgo_prints that needs to be introduced: wrapper of the C API generated by go, see prints.cgo2.c
CP prints.so/home/lizh/go/pkg/linux_386/Put the compiled dynamic library into the Go package directory
You can then call prints the C function in go: Package Main
import "prints"
Func Main () { s: = "Hello world!"; prints. Prints (s); }
View the generated callers and see a reference to the two dynamic libraries of $goroot/pkg/linux_386/libcgo.so and $goroot/pkg/linux_386/prints.so. The two libraries need to be placed in the publishing environment when publishing
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.