Go語言中CGO的使用實踐
部門產(chǎn)品業(yè)務(wù)功能采用Golang開發(fā),但是有些功能是用c寫的,比如說net-snmp,bfd協(xié)議等等,像這些如果使用GO語言重編的話,既有實現(xiàn)的復(fù)雜度也需要相當(dāng)長的時間,好在GO語言提供了CGO機制,使得能夠在go代碼中直接調(diào)用C的庫函數(shù),大大提高了效率,減少了重復(fù)開發(fā)工作,此外還支持在C語言中調(diào)用GO函數(shù),這一點還是蠻強大的。
1. Go語言調(diào)用C函數(shù)例子:
package main
//
// 引用的C頭文件需要在注釋中聲明,緊接著注釋需要有import "C",且這一行和注釋之間不能有空格
//
/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void myprint(char* s) {
printf("%s\n", s);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
//使用C.CString創(chuàng)建的字符串需要手動釋放。
cs := C.CString("Hello World\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
fmt.Println("call C.sleep for 3s")
C.sleep(3)
return
}
運行:

2. Go語言調(diào)用C庫函數(shù):
hello.c
#include <stdio.h>
void hello()
{
printf("hello world\n");
}
hello.h
#ifndef HELLO_H #define HELLO_H void hello(void); #endif
編譯:
gcc -c hello.c ar -cru libhello.a hello.o
package main
//使用#cgo定義庫路徑
/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -L . -lhello
#include "hello.h"
*/
import "C"
func main() {
C.hello()
}
運行:

3. Go語言導(dǎo)出函數(shù)給C語言使用:
main.go
package main
//
//#include <stdio.h>
//int add(int a, int b);
//
import "C"
import (
"fmt"
)
//當(dāng)使用export的時候,在同一個文件中就不能再定義其它的c函數(shù)了,不然會報錯。
//使用export導(dǎo)出函數(shù)給c語言調(diào)用。
//export GoAdd
func GoAdd(a, b int) int {
return a + b
}
func main() {
a := C.add(1, 2)
fmt.Printf("C.add(1,2) return %d\n", a)
}
cfunc.go
package main
//
//int GoAdd(int a, int b);
//
//int add(int a, int b)
//{
// return GoAdd(a,b);
//}
//
import "C"
運行:

4. Go語言導(dǎo)出函數(shù)指針給c語言使用:
還有一種使用方式,這種是我使用比較多的。就是傳遞函數(shù)指針,因為GO函數(shù)無法取址,因此需要寫個中間函數(shù)做個轉(zhuǎn)換操作,例子如下:
clibrary.c
#include <stdio.h>
#include "clibrary.h"
//參數(shù)是函數(shù)指針
void some_c_func(callback_fcn callback)
{
int arg = 2;
printf("C.some_c_func(): calling callback with arg = %d\n", arg);
int response = callback(2);
printf("C.some_c_func(): callback responded with %d\n", response);
}
clibrary.h
#ifndef CLIBRARY_H #define CLIBRARY_H //定義函數(shù)指針 typedef int (*callback_fcn)(int); void some_c_func(callback_fcn); #endif
Go code:
package main
/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -L . -lclibrary
#include "clibrary.h"
int callOnMeGo_cgo(int in); // 聲明
*/
import "C"
import (
"fmt"
"unsafe"
)
//export callOnMeGo
func callOnMeGo(in int) int {
return in + 1
}
func main() {
fmt.Printf("Go.main(): calling C function with callback to us\n")
//使用unsafe.Pointer轉(zhuǎn)換
C.some_c_func((C.callback_fcn)(unsafe.Pointer(C.callOnMeGo_cgo)))
}
中間函數(shù):
package main
/*
#include <stdio.h>
int callOnMeGo(int);
// The gateway function
int callOnMeGo_cgo(int in)
{
printf("C.callOnMeGo_cgo(): called with arg = %d\n", in);
//調(diào)用GO函數(shù)
return callOnMeGo(in);
}
*/
import "C"
運行:

開發(fā)注意事項:
1. 在注釋和import”C”之間不能有空行
2. 使用C.CString函數(shù)轉(zhuǎn)換GoString為CString時要手動釋放該字符串。
3. CGO不支持使用變參的函數(shù),例如printf,如果要使用的話,可以寫個包裹函數(shù)m'yprintf,使用傳參的方式調(diào)用。
4. Go支持使用//export導(dǎo)出函數(shù)給C使用,但是有一點需要注意就是不能在export導(dǎo)出的同一個文件里定義c函數(shù),不然會出現(xiàn)
multiple definition of "xxx"編譯錯誤,如果函數(shù)非常tiny的話,還有一個方法是使用static inline 來聲明該函數(shù),如下:
package gocallback
import (
"fmt"
"sync"
)
/*
extern void go_callback_int(int foo, int p1);
// normally you will have to define function or variables
// in another separate C file to avoid the multiple definition
// errors, however, using "static inline" is a nice workaround
// for simple functions like this one.
static inline void CallMyFunction(int foo) {
go_callback_int(foo, 5);
}
*/
import "C"
參考資料:
1. https://github.com/golang/go/wiki/cgo
到此這篇關(guān)于Go語言中CGO的使用實踐的文章就介紹到這了,更多相關(guān)Go語言使用CGO內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于微服務(wù)框架go-micro開發(fā)gRPC應(yīng)用程序
這篇文章介紹了基于微服務(wù)框架go-micro開發(fā)gRPC應(yīng)用程序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Ruby序列化和持久化存儲(Marshal、Pstore)操作方法詳解
這篇文章主要介紹了Ruby序列化和持久化存儲(Marshal、Pstore)操作方法詳解,包括Ruby Marshal序列化,Ruby Pstore存儲,需要的朋友可以參考下2022-04-04
用Go+Vue.js快速搭建一個Web應(yīng)用(初級demo)
這篇文章主要介紹了用Go+Vue.js快速搭建一個Web應(yīng)用(初級demo),本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友參考下吧2017-11-11

