C语言和go语言之间的交互操作方法

 更新时间:2020年4月25日 17:31  点击:1856

一、go代码中使用C代码

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

NewImage

代码示例:

go代码:testC.go

package main
/*
#include <stdio.h>
#include <stdlib.h>
void c_print(char *str) {
 printf("%s\n", str);
}
*/
import "C"  //import “C” 必须单起一行,并且紧跟在注释行之后
import "unsafe"

func main() {
 s := "Hello Cgo"
 cs := C.CString(s)//字符串映射
 C.c_print(cs)//调用C函数
 defer C.free(unsafe.Pointer(cs))//释放内存
}

运行结果:

$ 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

package main
import "C"
import "fmt"
//export go_print
func go_print(value string) {
  fmt.Println(value)
}
func main() {//main函数是必须的 有main函数才能让cgo编译器去把包编译成C的库
}

讲解:

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

#include “nautilus.h”//引入go代码导出的生成的C头文件
#include <stdio.h>

int main() {
 char cvalue[] = "Hello This is a C Application";
 int length = strlen(cvalue);
 GoString value = {cvalue, length};//go中的字符串类型在c中为GoString
 go_print(value);
 return 0;
}

编译步骤

// 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

#ifndef HELLO_H
#define HELLO_H


#include <stdio.h>
#include <stdlib.h>7
void go_print_c(char *str);

#endif

被go调用的C代码 hello.c

#include "hello.h"

void go_print_c(char *str) {
  printf("%s\n", str);
}

被C调用的go代码 print.go

package main

//#include "hello.h"
import "C"

//export go_print
func go_print(value string) {
 cs := C.CString(value)
 C.go_print_c(cs)
}

func main() {
}

讲解:

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

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

p.go

package main

/*
#include <stdio.h>
#include <stdlib.h>
void go_print_c(char *str) {
 printf("%s\n", str);
}
*/
import "C"
import "unsafe"

//export go_print
func go_print(value string) {
 cs := C.CString(value)
 C.go_print_c(cs)
}
... 

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

#include "nautilus.h"
#include3
int main() {
 printf("This is a C Application.\n");
 char cvalue[] = "hello world";
 int length = strlen(cvalue);
 GoString value = {cvalue, length};
 go_print(value);
 return 0;
}

编译步骤:

// 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

以上这篇C语言和go语言之间的交互操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章