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

1、go语言中使用C语言html

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

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 }

运行结果:ui

$ go run testC.go
Hello Cgogoogle

 

讲解:spa

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

若是执行go run **时出现 指针

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

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

二、import “C” 并无导入一个名为C的包,这里的import “C”相似于告诉Cgo将以前注释块中的C代码生成一段具备包装性质的Go代码

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

四、对于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字符串

须要注意的是每次转换都会致使一次内存复制,因此字符串的内容是不能够修改的

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

 

 

2、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 }

 

讲解:

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

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

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

四、命令执行完毕后会生成两个文件 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行 #include “nautilus.h"包含go代码导出生成的C头文件

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

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

 

3、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 }

 

讲解:

一、这里在函数前面加上了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

 

4、参考文献

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)中的坑

相关文章
相关标签/搜索