http://tonybai.com/2012/09/26/interoperability-between-go-and-c/
// foo.h int count; void foo(); //foo.c #include "foo.h" int count = 6; void foo() { printf("I am foo!\n"); }
//foo.go package main // #cgo LDFLAGS: -L ./ -lfoo // #include <stdio.h> // #include <stdlib.h> // #include "foo.h" import "C" import "fmt“ func main() { fmt.Println(C.count) C.foo() }
使用靜態庫
$> gcc -c foo.c
$> ar rv libfoo.a foo.o
[diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go [diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo 6 I am foo!
[diego@localhost ~/GoWork/src/applycation/testCgo]# gcc -fPIC -shared -o libfoo.so foo.c [diego@localhost ~/GoWork/src/applycation/testCgo]# rm li libfoo.a libfoo.so* [diego@localhost ~/GoWork/src/applycation/testCgo]# rm libfoo.a [diego@localhost ~/GoWork/src/applycation/testCgo]# go build fo foo* foo.c foo.go foo.h foo.o [diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo. foo.c foo.go foo.h foo.o [diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go [diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo 6 I am foo! [diego@localhost ~/GoWork/src/applycation/testCgo]#
http://tonybai.com/2012/09/26/interoperability-between-go-and-c/
與在Go中使用C源碼相比,在C中使用Go函數的場合較少。在Go中,可以使用"export + 函數名"來導出Go函數為C所使用,看一個簡單例子:
package main
/*
#include <stdio.h>
extern void GoExportedFunc();
void bar() {
printf("I am bar!\n");
GoExportedFunc();
}
*/
import "C"
import "fmt"
//export GoExportedFunc
func GoExportedFunc() {
fmt.Println("I am a GoExportedFunc!")
}
func main() {
C.bar()
}
不過當我們編譯該Go文件時,我們得到了如下錯誤信息:
# command-line-arguments
/tmp/go-build163255970/command-line-arguments/_obj/bar.cgo2.o: In function `bar':
./bar.go:7: multiple definition of `bar'
/tmp/go-build163255970/command-line-arguments/_obj/_cgo_export.o:/home/tonybai/test/go/bar.go:7: first defined here
collect2: ld returned 1 exit status
代碼似乎沒有任何問題,但就是無法通過編譯,總是提示“多重定義”。翻看Cgo的文檔,找到了些端倪。原來
There is a limitation: if your program uses any //export directives, then the C code in the comment may only include declarations (extern int f();), not definitions (int f() { return 1; }).
似乎是// extern int f()與//export f不能放在一個Go源文件中。我們把bar.go拆分成bar1.go和bar2.go兩個文件:
// bar1.go
package main
/*
#include <stdio.h>
extern void GoExportedFunc();
void bar() {
printf("I am bar!\n");
GoExportedFunc();
}
*/
import "C"
func main() {
C.bar()
}
// bar2.go
package main
import "C"
import "fmt"
//export GoExportedFunc
func GoExportedFunc() {
fmt.Println("I am a GoExportedFunc!")
}
編譯執行:
$> go build -o bar bar1.go bar2.go
$> bar
I am bar!
I am a GoExportedFunc!
Go代碼:
func TestCallback() { f1 := syscall.NewCallback(PlusOne) f2 := syscall.NewCallbackCDecl(PlusTwo) var m uint32 = 20 var n uint32 = 80 // Func1 __stdcall fmt.Println(C.Func1(C.uint(m), (*[0]byte)(unsafe.Pointer(f1)))) // 21 // Func2 __cdecl fmt.Println(C.Func2(C.uint(n), (*[0]byte)(unsafe.Pointer(f2)))) // 82 } func PlusOne(n uint32) uintptr { return uintptr(n + 1) } func PlusTwo(n uint32) uintptr { return uintptr(n + 2) }
C.Func1的第二個參數類型為函數,所以要傳入一個*[0]byte。 http://studygolang.com/articles/2629
http://blog.giorgis.io/cgo-examples