C语言和go语言之间的交互 - C语言中使用go语言,使用的go语言又使用了c语言

一、go语言中使用C语言

go代码中使用C代码,在go语言的函数块中,以注释的方式写入C代码,然后紧跟import “C” 即可在go代码中使用C函数

NewImage

代码示例:

go代码:testC.go

  1 package main
  2
  3 /*
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 void c_print(char *str) {
  7     printf("%s\n", str);
  8 }
  9 */
 10 import "C"   //import “C” 必须单起一行,并且紧跟在注释行之后
 11 import "unsafe"
 12
 13 func main() {
 14     s := "Hello Cgo"
 15     cs := C.CString(s)//字符串映射
 16     C.c_print(cs)//调用C函数
 17     defer C.free(unsafe.Pointer(cs))//释放内存
 18 }

运行结果:

$ go run testC.go
Hello Cgo

 

讲解:

1、go代码中的C代码,需要用注释包裹,块注释和行注释均可,其次import “C”是必须的,并且和上面的C代码之间不能用空行分割,必须紧密相连

如果执行go run **时出现 

# command-line-arguments
could not determine kind of name for  xxx

那么就需要考虑 是不是improt “C”和上面的C代码没有紧挨着导致了

2、import “C” 并没有导入一个名为C的包,这里的import “C”类似于告诉Cgo将之前注释块中的C代码生成一段具有包装性质的Go代码

3、访问C语言中的函数需要在前面加上C.前缀,如C.Cstring C.go_print C.free

4、对于C语中的原生类型,Cgo都有对应的Go语言中的类型 如go代码中C.int,C.char对应于c语言中的int,signed char,而C语言中void*指针在Go语言中用特殊的unsafe.Pointer(cs)来对应

而Go语言中的string类型,在C语言中用字符数组来表示,二者的转换需要通过go提供的一系列函数来完成:

C.Cstring      : 转换go的字符串为C字符串,C中的字符串是使用malloc分配的,所以需要调用C.free来释放内存

C.Gostring    :  转换C字符串为go字符串

C.GoStringN : 转换一定长度的C字符串为go字符串

需要注意的是每次转换都会导致一次内存复制,所以字符串的内容是不可以修改的

5、17行 利用defer C.free 和unsafe.Pointer显示释放调用C.Cstring所生成的内存块

 

 

二、C语言中使用go语言

NewImage

代码示例:

go代码:print.go

  1 package main
  2
  3 import "C"
  4 import "fmt"
  5
  6 //export go_print
  7 func go_print(value string) {
  8     fmt.Println(value)
  9 }
 10
 11 func main() {//main函数是必须的 有main函数才能让cgo编译器去把包编译成C的库
 12 }

 

讲解:

1、第11行 这里go代码中的main函数是必须的,有main函数才能让cgo编译器去把包编译成c的库

2、第3行 import “C”是必须的,如果没有import “C” 将只会build出一个.a文件,而缺少.h文件

3、第6行 //exoort go_print  这里的go_print要和下面的的go函数名一致,并且下面一行即为要导出的go函数

4、命令执行完毕后会生成两个文件 nautilus.a nautilus.h

nautilus.h中定义了go语言中的类型在C中对应的类型 和导出的go函数的函数声明

如:

typedef signed char GoInt8;//对应go代码中的int8类型

typedef struct { const char *p; GoInt n; } GoString;//对应go中的字符串

extern void go_print(GoString p0);//go中导出的函数的函数声明

 

C代码: c_go.c

  1 #include “nautilus.h”//引入go代码导出的生成的C头文件
  2 #include <stdio.h>
  3
  4 int main() {
  5   char cvalue[] = "Hello This is a C Application";
  6   int length = strlen(cvalue);
  7   GoString value = {cvalue, length};//go中的字符串类型在c中为GoString
  8   go_print(value);
  9   return 0;
 10 }

编译步骤

// as c-shared library
$ go build -buildmode=c-shared -o nautilus.a print.go

或者

// as c-archive 
$ go build -buildmode=c-archive -o nautilus.a print.go

$ gcc -o c_go c_go.c nautilus.a

运行结果

$ ./c_go
Hello This is a C Application

 

讲解:

1、第1行 #include “nautilus.h"包含go代码导出生成的C头文件

2、第7行 go中字符串类型在c中为GoString 定义为typedef struct { const char *p; GoInt n; } GoString; p为字符串指针,n为长度;所以这里通过GoString value = {cavalue, length}将C中的char赋值给GoString

3、第8行 go_print调用对应函数

 

三、C语言中使用go语言,使用的go语言又使用了c语言

NewImage

代码示例:

被go调用的C代码 hello.h

  1 #ifndef HELLO_H
  2 #define HELLO_H
  3
  4
  5 #include <stdio.h>
  6 #include <stdlib.h>7
  8 void go_print_c(char *str);
  9
 10 #endif

 

被go调用的C代码 hello.c

1 #include "hello.h"
  2
  3 void go_print_c(char *str) {
  4     printf("%s\n", str);
  5 }

 

被C调用的go代码 print.go

  1 package main
  2
  3 //#include "hello.h"
  4 import "C"
  5
  6 //export go_print
  7 func go_print(value string) {
  8     cs := C.CString(value)
  9     C.go_print_c(cs)
 10 }
 11
 12 func main() {
 13 }

 

讲解:

1、这里在函数前面加上了inline关键字

如果把C代码放入go代码注释块中并且没有inline关键字中,会出现重定义的错误

p.go

  1 package main
  2
  3 /*
  4  #include <stdio.h>
  5  #include <stdlib.h>
  6 void go_print_c(char *str) {
  7     printf("%s\n", str);
  8 }
  9 */
 10 import "C"
 11 import "unsafe"
 12
 13 //export go_print
 14 func go_print(value string) {
 15     cs := C.CString(value)
 16     C.go_print_c(cs)
 17 }
 18 ... 

 go build -buildmode=c-shared -o nautilus.a print.go执行失败

duplicate symbol _go_print_c in:
$WORK/_/Users/baidu/go_code/t/_obj/_cgo_export.o
$WORK/_/Users/baidu/go_code/t/_obj/p.cgo2.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决办法是给函数加上inline或者static关键字将函数改成内部链接,或者是像上面那样include头文件

 

C代码 _c_go.c

 

  1 #include "nautilus.h"
  2 #include3
  4 int main() {
  5   printf("This is a C Application.\n");
  6   char cvalue[] = "hello world";
  7   int length = strlen(cvalue);
  8   GoString value = {cvalue, length};
  9   go_print(value);
 10   return 0;
 11 }

 

编译步骤:

// as c-shared library
$ go build -buildmode=c-shared -o nautilus.a 

或者

// as c-archive 
$ go build -buildmode=c-archive -o nautilus.a 

$ gcc -o c_go_c c_go.c nautilus.a

 

运行结果

$ ./c_go_c.o
This is a C Application.
hello world

 

四、参考文献

http://www.cnblogs.com/sevenyuan/p/4544294.html                                Go与C语言的互操作

http://blog.ralch.com/tutorial/golang-sharing-libraries/                              Sharing Golang packages to C and Go

https://groups.google.com/forum/#!topic/golang-china/vUd4Civs_Bs         google go论坛

http://blog.csdn.net/u014633283/article/details/52225274                       GO中调用C代码(CGO)中的坑

 

 

 

 

posted on 2017-06-13 17:30  magicsoar  阅读(15547)  评论(0编辑  收藏  举报

导航