golang go语言与C语言互调,通过cgo

htyu_0203_39 · · 7221 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

1. good links:


http://tonybai.com/2012/09/26/interoperability-between-go-and-c/
http://www.mischiefblog.com/2014/06/26/example-cgo-golang-app-that-calls-a-native-library-with-a-c-structure/
http://tonybai.com/2016/02/21/some-changes-in-go-1-6/
http://blog.giorgis.io/cgo-examples
http://tonybai.com/tag/cgo/
http://cholerae.com/2015/05/17/%E4%BD%BF%E7%94%A8Cgo%E7%9A%84%E4%B8%80%E7%82%B9%E6%80%BB%E7%BB%93/
http://akrennmair.github.io/golang-cgo-slides/#1


2. cgo rules for c/c++


2.1. Mapping the C namespace to Go:


2.1.1 Everything declared in the C code is available in the C pseudo-package
2.1.2 Fundamental C data types have their counterpart, e.g. int → C.int, unsigned short → C.ushort, etc.
2.1.3 The Go equivalent to void * is unsafe.Pointer
2.1.4 typedefs are available under their own name
2.1.5 structs are available with a struct_ prefix, e.g. struct foo → C.struct_foo, same goes for unions and enums

2.2. Conversion between C and Go strings:


2.2.1. The C package contains conversion functions to convert Go to C strings and vice versa
2.2.2. Also: opaque data (behind void *) to []byte
2.2.3. Go string to C string; result must be freed with C.free : func C.CString(string) *C.char
2.2.4. C string to Go string: func C.GoString(*C.char) string
2.2.5. C string, length to Go string: func C.GoStringN(*C.char, C.int) string
2.2.6. C pointer, length to Go []byte: func C.GoBytes(unsafe.Pointer, C.int) []byte

2.3. Go调用C Code时,Go传递给C Code的Go Pointer所指的Go Memory中不能包含任何指向Go Memory的Pointer:


2.3.1. 传递一个指向Struct的指针

//cgo1_struct.go
package main


/*
#include <stdio.h>
struct Foo{
    int a;
    int *p;
};


void plusOne(struct Foo *f) {
    (f->a)++;
    *(f->p)++;
}
*/
import "C"
import "unsafe"
import "fmt"


func main() {
    f := &C.struct_Foo{}
    f.a = 5
    f.p = (*C.int)((unsafe.Pointer)(new(int)))
    //f.p = &f.a


    C.plusOne(f)
    fmt.Println(int(f.a))
}
从cgo1_struct.go代码中可以看到,Go code向C code传递了一个指向Go Memory(Go分配的)指针f,
但f指向的Go Memory中有一个指针p指向了另外一处Go Memory: new(int),我们来run一下这段代码:
$go run cgo1_struct.go
# command-line-arguments
./cgo1_struct.go:12:2: warning: expression result unused [-Wunused-value]
panic: runtime error: cgo argument has Go pointer to Go pointer


goroutine 1 [running]:
panic(0x4068400, 0xc82000a110)
    /Users/tony/.bin/go16/src/runtime/panic.go:464 +0x3e6
main.main()
    /Users/tony/test/go/go16/cgo/cgo1_struct.go:24 +0xb9
exit status 2
代码出现了Panic,并提示:“cgo argument has Go pointer to Go pointer”。我们的代码违背了Cgo Pointer传递规则,
即便让f.p指向struct自身内存也是不行的,比如f.p = &f.a。

2.3.2. 传递一个指向struct field的指针:

按照rules中的说明,如果传递的是一个指向struct field的指针,那么”Go Memory”专指这个field所占用的内存,
即便struct中有其他field指向其他Go memory也不打紧:

//cgo1_structfield.go
package main


/*
#include <stdio.h>
struct Foo{
    int a;
    int *p;
};


void plusOne(int *i) {
    (*i)++;
}
*/
import "C"
import (
    "fmt"
    "unsafe"
)


func main() {
    f := &C.struct_Foo{}
    f.a = 5
    f.p = (*C.int)((unsafe.Pointer)(new(int)))


    C.plusOne(&f.a)
    fmt.Println(int(f.a))
}
上述程序的运行结果:


$go run cgo1_structfield.go
6

2.3.3. 传递一个指向slice or array中的element的指针:

和传递struct field不同,传递一个指向slice or array中的element的指针时,
需要考虑的Go Memory的范围不仅仅是这个element,
而是整个Array或整个slice背后的underlying array所占用的内存区域,
要保证这个区域内不包含指向任意Go Memory的指针。我们来看代码示例:
//cgo1_sliceelem.go
package main


/*
#include <stdio.h>
void plusOne(int **i) {
    (**i)++;
}
*/
import "C"
import (
    "fmt"
    "unsafe"
)


func main() {
    sl := make([]*int, 5)
    var a int = 5
    sl[1] = &a
    C.plusOne((**C.int)((unsafe.Pointer)(&sl[0])))
    fmt.Println(sl[0])
}
从这个代码中,我们看到我们传递的是slice的第一个element的地址,即&sl[0]。我们并未给sl[0]赋值,
但sl[1] 被赋值为另外一块go memory的address(&a),当我们将&sl[0]传递给plusOne时,执行结果如下:
$go run cgo1_sliceelem.go
panic: runtime error: cgo argument has Go pointer to Go pointer


goroutine 1 [running]:
panic(0x40dbac0, 0xc8200621d0)
    /Users/tony/.bin/go16/src/runtime/panic.go:464 +0x3e6
main.main()
    /Users/tony/test/go/go16/cgo/cgo1_sliceelem.go:19 +0xe4
exit status 2

2.4. C调用Go Code时:

2.4.1. C调用的Go函数不能返回指向Go分配的内存的指针:

package main


// extern int* goAdd(int, int);
//
// static int cAdd(int a, int b) {
//     int *i = goAdd(a, b);
//     return *i;
// }
import "C"
import "fmt"


//export goAdd
func goAdd(a, b C.int) *C.int {
    c := a + b
    return &c
}


func main() {
    var a, b int = 5, 6
    i := C.cAdd(C.int(a), C.int(b))
    fmt.Println(int(i))
}


可以看到:goAdd这个Go函数返回了一个指向Go分配的内存(&c)的指针。运行上述代码,结果如下:
$go run cgo2_1.go
panic: runtime error: cgo result has Go pointer


goroutine 1 [running]:
panic(0x40dba40, 0xc82006e1c0)
    /Users/tony/.bin/go16/src/runtime/panic.go:464 +0x3e6
main._cgoexpwrap_872b2f2e7532_goAdd.func1(0xc820049d98)
    command-line-arguments/_obj/_cgo_gotypes.go:64 +0x3a
main._cgoexpwrap_872b2f2e7532_goAdd(0x600000005, 0xc82006e19c)
    command-line-arguments/_obj/_cgo_gotypes.go:66 +0x89
main._Cfunc_cAdd(0x600000005, 0x0)
    command-line-arguments/_obj/_cgo_gotypes.go:45 +0x41
main.main()
    /Users/tony/test/go/go16/cgo/cgo2_1.go:20 +0x35
exit status 2

2.4.2. Go code不能在C分配的内存中存储指向Go分配的内存的指针:

//cgo2_2.go
package main


// #include <stdlib.h>
// extern void goFoo(int**);
//
// static void cFoo() {
//     int **p = malloc(sizeof(int*));
//     goFoo(p);
// }
import "C"


//export goFoo
func goFoo(p **C.int) {
    *p = new(C.int)
}


func main() {
    C.cFoo()
}
不过针对此例,默认的GODEBUG=cgocheck=1偏是无法check出问题。我们将GODEBUG=cgocheck改为=2试试:
$GODEBUG=cgocheck=2 go run cgo2_2.go
write of Go pointer 0xc82000a0f8 to non-Go memory 0x4300000
fatal error: Go pointer stored into non-Go memory


runtime stack:
runtime.throw(0x4089800, 0x24)
    /Users/tony/.bin/go16/src/runtime/panic.go:530 +0x90
runtime.cgoCheckWriteBarrier.func1()
    /Users/tony/.bin/go16/src/runtime/cgocheck.go:44 +0xae
runtime.systemstack(0x7fff5fbff8c0)
    /Users/tony/.bin/go16/src/runtime/asm_amd64.s:291 +0x79
runtime.mstart()
    /Users/tony/.bin/go16/src/runtime/proc.go:1048
... ...
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /Users/tony/.bin/go16/src/runtime/asm_amd64.s:1998 +0x1
exit status 2
果真runtime panic: write of Go pointer 0xc82000a0f8 to non-Go memory 0×4300000






2.3. Calling Go code from C (1)


Go code can be exported via comment: //export <function-name>
It can then be called from C
package foo


/*
extern void myprint(int i);


void dofoo(void) {
int i;
for (i=0;i<10;i++) {
myprint(i);
}
}
*/
import "C"

//export myprint
func myprint(i C.int) {
fmt.Printf("i = %v\n", uint32(i))
}


func DoFoo() {
C.dofoo()
}


3. cgo examples:


package pwnam


//basic rule 1: 所有C代码都要放到注释当中.
/*
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
*/


//basic rule 2: 必须导入"C", "unsafe"包,用于go与c互操作.
import ( "C" ; "unsafe" )


type Passwd struct {
Uid uint32 ; Gid uint32 ; Dir string ; Shell string
}


func Getpwnam(name string) *Passwd {
cname := C.CString(name) //basic rule 3: go string to c string.
defer C.free(unsafe.Pointer(cname)) //basic rule 4: must clean mannually and obviously.
cpw := C.getpwnam(cname) //basic rule 5: call C function, types, variables and etc all by C namespace.

return &Passwd{
Uid: uint32(cpw.pw_uid), Gid: uint32(cpw.pw_uid),
Dir: C.GoString(cpw.pw_dir), Shell: C.GoString(cpw.pw_shell)} //basic rule 6: c string to go string.
}


4. cgo compilation and link example:


#cgo LDFLAGS:
package pcap
/*
#cgo LDFLAGS: -lpcap
#include <stdlib.h>
#include <pcap.h>
*/
import "C"
#cgo pkg-config:
package stfl
/*
#cgo pkg-config: stfl
#cgo LDFLAGS: -lncursesw
#include <stdlib.h>
#include <stfl.h>
*/
import "C"




5. cgo 内存管理大原则


go runtime只管go的内存, 不管C,反之C只管自己也不管go

所以go的内存有GC照看, C的内存要自己C.free。



待续。。。


注意: 此文章只是我个人笔记, 如有错漏,请一定指正, 共同学习, 我的邮箱: htyu_0203_39@sina.com









有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:htyu_0203_39

查看原文:golang go语言与C语言互调,通过cgo

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

7221 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传