一、go語言中使用C語言
go代碼中使用C代碼,在go語言的函數(shù)塊中,以注釋的方式寫入C代碼,然后緊跟import “C” 即可在go代碼中使用C函數(shù)
代碼示例:
go代碼:testC.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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) //調(diào)用C函數(shù) 17 defer C. free (unsafe.Pointer(cs)) //釋放內(nèi)存 18 } |
運(yùn)行結(jié)果:
$ go run testC.go
Hello Cgo
講解:
1、go代碼中的C代碼,需要用注釋包裹,塊注釋和行注釋均可,其次import “C”是必須的,并且和上面的C代碼之間不能用空行分割,必須緊密相連
如果執(zhí)行g(shù)o run **時(shí)出現(xiàn)
# command-line-arguments
could not determine kind of name for xxx
那么就需要考慮 是不是improt “C”和上面的C代碼沒有緊挨著導(dǎo)致了
2、import “C” 并沒有導(dǎo)入一個(gè)名為C的包,這里的import “C”類似于告訴Cgo將之前注釋塊中的C代碼生成一段具有包裝性質(zhì)的Go代碼
3、訪問C語言中的函數(shù)需要在前面加上C.前綴,如C.Cstring C.go_print C.free
4、對于C語中的原生類型,Cgo都有對應(yīng)的Go語言中的類型 如go代碼中C.int,C.char對應(yīng)于c語言中的int,signed char,而C語言中void*指針在Go語言中用特殊的unsafe.Pointer(cs)來對應(yīng)
而Go語言中的string類型,在C語言中用字符數(shù)組來表示,二者的轉(zhuǎn)換需要通過go提供的一系列函數(shù)來完成:
C.Cstring : 轉(zhuǎn)換go的字符串為C字符串,C中的字符串是使用malloc分配的,所以需要調(diào)用C.free來釋放內(nèi)存
C.Gostring : 轉(zhuǎn)換C字符串為go字符串
C.GoStringN : 轉(zhuǎn)換一定長度的C字符串為go字符串
需要注意的是每次轉(zhuǎn)換都會(huì)導(dǎo)致一次內(nèi)存復(fù)制,所以字符串的內(nèi)容是不可以修改的
5、17行 利用defer C.free 和unsafe.Pointer顯示釋放調(diào)用C.Cstring所生成的內(nèi)存塊
二、C語言中使用go語言
代碼示例:
go代碼:print.go
1 2 3 4 5 6 7 8 9 10 11 12 | 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函數(shù)是必須的 有main函數(shù)才能讓cgo編譯器去把包編譯成C的庫 12 } |
講解:
1、第11行 這里go代碼中的main函數(shù)是必須的,有main函數(shù)才能讓cgo編譯器去把包編譯成c的庫
2、第3行 import “C”是必須的,如果沒有import “C” 將只會(huì)build出一個(gè).a文件,而缺少.h文件
3、第6行 //exoort go_print 這里的go_print要和下面的的go函數(shù)名一致,并且下面一行即為要導(dǎo)出的go函數(shù)
4、命令執(zhí)行完畢后會(huì)生成兩個(gè)文件 nautilus.a nautilus.h
nautilus.h中定義了go語言中的類型在C中對應(yīng)的類型 和導(dǎo)出的go函數(shù)的函數(shù)聲明
如:
typedef signed char GoInt8;//對應(yīng)go代碼中的int8類型
typedef struct { const char *p; GoInt n; } GoString;//對應(yīng)go中的字符串
extern void go_print(GoString p0);//go中導(dǎo)出的函數(shù)的函數(shù)聲明
C代碼: c_go.c
1 2 3 4 5 6 7 8 9 10 | 1 #include “nautilus.h” //引入go代碼導(dǎo)出的生成的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
運(yùn)行結(jié)果
$ ./c_go
Hello This is a C Application
講解:
1、第1行 #include “nautilus.h"包含go代碼導(dǎo)出生成的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調(diào)用對應(yīng)函數(shù)
三、C語言中使用go語言,使用的go語言又使用了c語言
代碼示例:
被go調(diào)用的C代碼 hello.h
1 2 3 4 5 6 7 8 9 | 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調(diào)用的C代碼 hello.c
1 2 3 4 5 | 1 #include "hello.h" 2 3 void go_print_c( char *str) { 4 printf ( "%s\n" , str); 5 } |
被C調(diào)用的go代碼 print.go
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 } |
講解:
1、這里在函數(shù)前面加上了inline關(guān)鍵字
如果把C代碼放入go代碼注釋塊中并且沒有inline關(guān)鍵字中,會(huì)出現(xiàn)重定義的錯(cuò)誤
p.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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執(zhí)行失敗
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)
解決辦法是給函數(shù)加上inline或者static關(guān)鍵字將函數(shù)改成內(nèi)部鏈接,或者是像上面那樣include頭文件
C代碼 _c_go.c
1 2 3 4 5 6 7 8 9 10 | 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
運(yùn)行結(jié)果
$ ./c_go_c.o
This is a C Application.
hello world
http://www.cnblogs.com/magicsoar/p/7002467.html