Go 1.5发布后,其包含一个特性:能够编译生成C语言动态连接库或静态库。本文给出了示例代码和用法。golang
go build
和go install
命令,能够使用参数 -buildmode
来指定生成哪一种类型的二进制目标文件。请见https://golang.org/cmd/go/#Description of build modes 详细说明。测试
当前咱们使用 -buildmode=c-archive
来示例和测试。ui
Golang源文件:spa
// file hello.go package main port "C" import "fmt" //export SayHello func SayHello(name string) { fmt.Printf("func in Golang SayHello says: Hello, %s!\n", name) } //export SayHelloByte func SayHelloByte(name []byte) { fmt.Printf("func in Golang SayHelloByte says: Hello, %s!\n", string(name)) } //export SayBye func SayBye() { fmt.Println("func in Golang SayBye says: Bye!") } func main() { // We need the main function to make possible // CGO compiler to compile the package as C shared library }
使用命令go build -buildmode=c-archive -o libhello.a hello.go
能够生成一个C语言静态库libhello.a
和头文件libhello.h
。 而后咱们再写个C语言程序来调用这个库,以下:code
// file hello.c #include <stdio.h> #include "libhello.h" int main() { printf("This is a C Application.\n"); GoString name = {(char*)"Jane", 4}; SayHello(name); GoSlice buf = {(void*)"Jane", 4, 4}; SayHelloByte(buf); SayBye(); return 0; }
使用命令gcc -o hello hello.c libhello.a -pthread
来编译生成一个可执行文件hello
。执行命令以下:blog
$ go build -buildmode=c-archive -o libhello.a hello.go $ gcc -o hello hello.c libhello.a -pthread $ ./hello This is a C Application. func in Golang SayHello says: Hello, Jane! func in Golang SayHelloByte says: Hello, Jane! func in Golang SayBye says: Bye!
备注:目前Golang还不支持将一个struct结构导出到C库中。ip