This is a creation in Article, where the information may have evolved or changed.
In many scenarios, you need to call the C function in the Go program or a library written in C (the underlying driver, algorithm, etc., do not want to use the go language to build the wheel again, re-use the existing C library).
So how do you call it? Go is a better C language Ah, of course, provides the function of C language interaction, called Cgo
!
Cgo
The #cgo
pseudo C grammar is encapsulated, and parameters are CFLAGS
used to pass in the compile option LDFLAGS
to pass in the link option. This third-party C library is used to invoke non-C standards.
1) First from the simplest to write, go code directly called the C function, the following example in the code comment block called the standard C library, and wrote a C function (in this case, simply print a word, in the comment block can write arbitrary C code), in the go Code part directly call the C functionhi()
package mainimport "fmt"/*#include <stdio.h>void hi() { printf("hello world!\n");}*/import "C" //这里可看作封装的伪包C, 这条语句要紧挨着上面的注释块,不可在它俩之间间隔空行!func main() { C.hi() fmt.Println("Hi, vim-go")}
Operation Result:
root@slave2:/home/cgo# go run main.go hello world!Hi, vim-go
Well, I can write C code in the Go code, so how do I call the compiled third-party C library directly in go? Use Cgo
!
2) This example shows a third-party dynamic library that calls non-standard C in Go code
C file
/* * hi.c * created on: July 1, 2017 * author: mark */#include <stdio.h>void hi() { printf("Hello Cgo!\n");}
H file
void hi();
Compiled into a dynamic library.
root@slave2:/home/cgo# gcc -c -fPIC -o hi.o hi.croot@slave2:/home/cgo# gcc -shared -o libhi.so hi.o
Go file
package mainimport "fmt"/*#cgo CFLAGS: -I./#cgo LDFLAGS: -L./ -lhi#include "hi.h" //非标准c头文件,所以用引号*/import "C"func main() { C.hi() fmt.Println("Hi, vim-go")}
The point comes (knocking the blackboard):
CFLAGS
The -I
(uppercase i) parameter indicates .h
the path where the header file is located
LDFLAGS
In -L
(uppercase) indicates that the path of the. so file -l
(lowercase l) indicates that the name of the library under that path is specified, for example libhi.so
, by using -lhi
(omitting the libhi.so
lib
and .so
characters in the For the specific meanings of these characters, please Google yourself.
Operation Result:
root@slave2:/home/cgo# go run main.go Hello Cgo!Hi, vim-go
A little bit deeper,
1) header file path and library file path write to die, once the third-party library installation path changes, Golang code will follow the changes, it would be very troublesome. In this case you can use the cgo
command pk-config
, please refer to this blog: Golang using Pkg-config to automatically get header files and link library method
2) When using the above method in Go, the host (or the cloud server) must have the corresponding .so
files, if not there will be linked error, causing the program to quit.
If it .so
is unnecessary third-party drive library (optional), then it is troublesome, you can not run this program, each host installed on the unnecessary third-party library. Is there a way to call these libraries when the GO program is running .so
, if it doesn't exist (do not enable the functionality provided by that library, rather than the link error exits directly)? Of course! Please look forward to an article. :)