Call the C dynamic Library in the Go function

Source: Internet
Author: User
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 !
CgoThe #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):
CFLAGSThe -I (uppercase i) parameter indicates .h the path where the header file is located
LDFLAGSIn -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. :)

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.