温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Go语言中CGO怎么用

发布时间:2021-09-24 11:56:46 来源:亿速云 阅读:202 作者:小新 栏目:开发技术

这篇文章主要介绍了Go语言中CGO怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1. Go语言调用C函数例子:

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创建的字符串需要手动释放。	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 }

运行:

Go语言中CGO怎么用

2. Go语言调用C库函数:

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() }

运行:

Go语言中CGO怎么用

3. Go语言导出函数给C语言使用:

main.go

package main   // //#include <stdio.h> //int add(int a, int b); // import "C"   import (	"fmt" )   //当使用export的时候,在同一个文件中就不能再定义其它的c函数了,不然会报错。 //使用export导出函数给c语言调用。   //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"

运行:

Go语言中CGO怎么用

4. Go语言导出函数指针给c语言使用:

还有一种使用方式,这种是我使用比较多的。就是传递函数指针,因为GO函数无法取址,因此需要写个中间函数做个转换操作,例子如下:

clibrary.c

#include <stdio.h>   #include "clibrary.h"   //参数是函数指针 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 //定义函数指针 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转换	C.some_c_func((C.callback_fcn)(unsafe.Pointer(C.callOnMeGo_cgo))) }

中间函数:

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);     //调用GO函数	return callOnMeGo(in); } */ import "C"

运行:

Go语言中CGO怎么用

开发注意事项:

1. 在注释和import”C”之间不能有空行

2. 使用C.CString函数转换GoString为CString时要手动释放该字符串。

3. CGO不支持使用变参的函数,例如printf,如果要使用的话,可以写个包裹函数m'yprintf,使用传参的方式调用。

4. Go支持使用//export导出函数给C使用,但是有一点需要注意就是不能在export导出的同一个文件里定义c函数,不然会出现

multiple definition of "xxx"编译错误,如果函数非常tiny的话,还有一个方法是使用static inline 来声明该函数,如下:

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"

感谢你能够认真阅读完这篇文章,希望小编分享的“Go语言中CGO怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI