Go言语中CGO的独霸幻想
部分产品营业下场采取Golang斥地 ,言语可是霸幻有些下场是用c写的 ,比如说net-snmp ,言语bfd和谈等等,霸幻像这些假定独霸GO言语重编的言语话 ,既有完成的霸幻宏壮度也需求相当长的时分,幸而GO言语供给了CGO机制 ,言语使得可以在go代码中直接调用C的霸幻库函数,除夜除夜进步了屈就,言语促进了几回斥地工作,霸幻此外还支撑在C言语中调用GO函数,这一点仍是言语蛮壮除夜的。
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}运转:

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.car -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言语导出函数给C言语独霸:
main.go
package main ////#include <stdio.h>//int add(int a,言语 int b);//import "C" import ( "fmt") //当独霸export的时辰,在不合个文件中就不克不及再定义此外的c函数了,不然会报错。//独霸export导出函数给c言语调用。 //export GoAddfunc 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言语导出函数指针给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 callOnMeGofunc 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 functionint callOnMeGo_cgo(int in){ printf("C.callOnMeGo_cgo(): called with arg = %d\n", in); //调用GO函数 return callOnMeGo(in);}*/import "C"运转:

斥地寄看事项:
1. 在诠释和import”C”之间不克不及有空行
2. 独霸C.CString函数转换GoString为CString时要手动释放该字符串。
3. CGO不支撑独霸变参的函数,比如printf,假定要独霸的话 ,可以写个包裹函数m'yprintf,独霸传参的编制调用 。
4. Go支撑独霸//export导出函数给C独霸 ,可是有一点需求寄看就是不克不及在export导出的不合个文件里定义c函数,不然会展示
multiple definition of "***"编译偏向,假定函数特别很是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 *** 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
到此这篇关于Go言语中CGo的独霸幻想的文章就引见到这了,更多相干Go言语独霸CGO内容请搜刮完竣下载之前的文章或延续不雅不雅不雅不雅鉴赏上面的相干文章希看大年夜师往后多多支撑完竣下载 !
部分产品营业下场采取Golang斥地 ,言语可是霸幻有些下场是用c写的 ,比如说net-snmp ,言语bfd和谈等等,霸幻像这些假定独霸GO言语重编的言语话 ,既有完成的霸幻宏壮度也需求相当长的时分,幸而GO言语供给了CGO机制 ,言语使得可以在go代码中直接调用C的霸幻库函数,除夜除夜进步了屈就,言语促进了几回斥地工作,霸幻此外还支撑在C言语中调用GO函数,这一点仍是言语蛮壮除夜的。
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}运转:

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.car -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言语导出函数给C言语独霸:
main.go
package main ////#include <stdio.h>//int add(int a,言语 int b);//import "C" import ( "fmt") //当独霸export的时辰,在不合个文件中就不克不及再定义此外的c函数了,不然会报错。//独霸export导出函数给c言语调用。 //export GoAddfunc 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言语导出函数指针给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 callOnMeGofunc 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 functionint callOnMeGo_cgo(int in){ printf("C.callOnMeGo_cgo(): called with arg = %d\n", in); //调用GO函数 return callOnMeGo(in);}*/import "C"运转:

斥地寄看事项:
1. 在诠释和import”C”之间不克不及有空行
2. 独霸C.CString函数转换GoString为CString时要手动释放该字符串。
3. CGO不支撑独霸变参的函数,比如printf,假定要独霸的话 ,可以写个包裹函数m'yprintf,独霸传参的编制调用 。
4. Go支撑独霸//export导出函数给C独霸 ,可是有一点需求寄看就是不克不及在export导出的不合个文件里定义c函数,不然会展示
multiple definition of "***"编译偏向,假定函数特别很是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 *** 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
到此这篇关于Go言语中CGo的独霸幻想的文章就引见到这了,更多相干Go言语独霸CGO内容请搜刮完竣下载之前的文章或延续不雅不雅不雅不雅鉴赏上面的相干文章希看大年夜师往后多多支撑完竣下载 !













