Go 1.5发布后,其包含一个特性:可以编译生成C语言动态链接库或静态库。本文给出了示例代码和用法。

go buildgo install-buildmode
-buildmode=c-archive

Golang源文件:


// 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.golibhello.alibhello.h
// 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 -pthreadhello
$ 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库中。

参考