1、go語言中使用C語言html
go代碼中使用C代碼,在go語言的函數塊中,以註釋的方式寫入C代碼,而後緊跟import 「C」 便可在go代碼中使用C函數golang
代碼示例:數組
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語言
代碼示例:
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語言
代碼示例:
被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)中的坑