使用C共享库从其他语言调用Go函数

只演示c和python

首先,让我们编写Go代码。 假设我们已经编写了一个Go库,希望将其提供给其他语言使用。 在将代码编译到共享库中之前,要遵循四个要求:

  • 该软件包必须是main包。 编译器会将软件包及其所有依赖项构建到单个共享库二进制文件中。
  • 源必须import "C"。
  • 使用//export注释来注释希望其他语言可以使用的功能。
  • 必须声明一个空的main函数。

以下Go源代码导出了四个函数Add,Cosine,Sort和Log。

package main

import "C"

import (
    "fmt"
    "math"
    "sort"
    "sync"
)

var count int
var mtx sync.Mutex

//export Add
func Add(a, b int) int {
    return a + b
}

//export Cosine
func Cosine(x float64) float64 {
    return math.Cos(x)
}

//export Sort
func Sort(vals []int) {
    sort.Ints(vals)
}

//export Log
func Log(msg string) int {
    mtx.Lock()
    defer mtx.Unlock()
    fmt.Println(msg)
    count++
    return count
}

func main() {}

使用-buildmode=c-shared build标志编译该软件包,以创建共享库二进制文件:


go build -o awesome.so -buildmode=c-shared awesome.go

最后生成awesome.h和awesome.so

c

有两种方法可以使用共享库从C调用Go函数。首先,我们可以在编译时静态绑定共享库,但可以在运行时动态链接它。或者,在运行时动态加载和绑定Go功能符号。

动态链接

在这种方法中,我们使用头文件来静态引用共享对象文件中导出的类型和函数。代码简单干净,如下所示:

#include <stdio.h>
#include "awesome.h"

int main() {
    printf("Using awesome lib from C:\n");
   
    //Call Add() - passing integer params, interger result
    GoInt a = 12;
    GoInt b = 99;
    printf("awesome.Add(12,99) = %d\n", Add(a, b)); 

    //Call Cosine() - passing float param, float returned
    printf("awesome.Cosine(1) = %f\n", (float)(Cosine(1.0)));
    
    //Call Sort() - passing an array pointer
    GoInt data[6] = {77, 12, 5, 99, 28, 23};
    GoSlice nums = {data, 6, 6};
    Sort(nums);
    printf("awesome.Sort(77,12,5,99,28,23): ");
    for (int i = 0; i < 6; i++){
        printf("%d,", ((GoInt *)nums.data)[i]);
    }
    printf("\n");

    //Call Log() - passing string value
    GoString msg = {"Hello from C!", 13};
    Log(msg);
}

接下来我们编译C代码,指定共享对象库:

$> gcc -o client client1.c ./awesome.so

当生成的二进制文件被执行时,它会链接到awesome。调用从Go导出的函数,如下所示。

$> ./client
awesome.Add(12,99) = 111
awesome.Cosine(1) = 0.540302
awesome.Sort(77,12,5,99,28,23): 5,12,23,28,77,99,
Hello from C!

Dynamically Loaded

在这种方法中,C代码使用动态链接加载器库(libdll .so)来动态加载和绑定导出的符号。它使用dhfcn.h,例如dlopen打开库文件,dlsym查找符号,dlerror检索错误,dlclose关闭共享库文件。

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

// define types needed
typedef long long go_int;
typedef double go_float64;
typedef struct{void *arr; go_int len; go_int cap;} go_slice;
typedef struct{const char *p; go_int len;} go_str;

int main(int argc, char **argv) {
    void *handle;
    char *error;

    // use dlopen to load shared object
    handle = dlopen ("./awesome.so", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }
    
    // resolve Add symbol and assign to fn ptr
    go_int (*add)(go_int, go_int)  = dlsym(handle, "Add");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }
    // call Add()
    go_int sum = (*add)(12, 99); 
    printf("awesome.Add(12, 99) = %d\n", sum);

    // resolve Cosine symbol
    go_float64 (*cosine)(go_float64) = dlsym(handle, "Cosine");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }
    // Call Cosine
    go_float64 cos = (*cosine)(1.0);
    printf("awesome.Cosine(1) = %f\n", cos);

    // resolve Sort symbol
    void (*sort)(go_slice) = dlsym(handle, "Sort");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }
    // call Sort
    go_int data[5] = {44,23,7,66,2};
    go_slice nums = {data, 5, 5};
    sort(nums);
    printf("awesome.Sort(44,23,7,66,2): ");
    for (int i = 0; i < 5; i++){
        printf("%d,", ((go_int *)data)[i]);
    }
    printf("\n");

    // resolve Log symbol
    go_int (*log)(go_str) = dlsym(handle, "Log");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }
    // call Log
    go_str msg = {"Hello from C!", 13};
    log(msg);
    
    // close file handle when done
    dlclose(handle);
}

在前面的代码中,我们定义了兼容Go的C类型go_intgo_floatgo_slicego_str的子集。我们使用dlsym加载符号AddCosineSortLog,并将它们分配给各自的函数指针。接下来,我们编译将其与dl库(不是awesome.so)连接的代码,如下所示:

$> gcc -o client client2.c -ldl
$> ./client
awesome.Add(12, 99) = 111
awesome.Cosine(1) = 0.540302
awesome.Sort(44,23,7,66,2): 2,7,23,44,66,
Hello from C!

Python

Python 我们使用 ctypes foreign function library 来调用 awesome.so

from __future__ import print_function
from ctypes import *

lib = cdll.LoadLibrary("./awesome.so")

# describe and invoke Add()
lib.Add.argtypes = [c_longlong, c_longlong]
lib.Add.restype = c_longlong
print("awesome.Add(12,99) = %d" % lib.Add(12,99))

# describe and invoke Cosine()
lib.Cosine.argtypes = [c_double]
lib.Cosine.restype = c_double
print("awesome.Cosine(1) = %f" % lib.Cosine(1))

# define class GoSlice to map to:
# C type struct { void *data; GoInt len; GoInt cap; }
class GoSlice(Structure):
    _fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]

nums = GoSlice((c_void_p * 5)(74, 4, 122, 9, 12), 5, 5) 

# call Sort
lib.Sort.argtypes = [GoSlice]
lib.Sort.restype = None
lib.Sort(nums)
print("awesome.Sort(74,4,122,9,12) = %s" % [nums.data[i] for i in range(nums.len)])

# define class GoString to map:
# C type struct { const char *p; GoInt n; }
class GoString(Structure):
    _fields_ = [("p", c_char_p), ("n", c_longlong)]

# describe and call Log()
lib.Log.argtypes = [GoString]
lib.Log.restype = c_longlong
msg = GoString(b"Hello Python!", 13)
print("log id %d"% lib.Log(msg))
$> python client.py
awesome.Add(12,99) = 111
awesome.Cosine(1) = 0.540302
awesome.Sort(74,4,122,9,12) = [4, 9, 12, 74, 122]
Hello Python!
log id 1

Python CFFI (contributed)

The following example was contributed by @sbinet (thank you!)

Python还有一个可移植的CFFI库,可以在Python2/Python3/pypy下正常工作。的
下面的示例使用c包装器定义导出的Go类型。这使得python示例更容易理解。

from __future__ import print_function
import sys
from cffi import FFI

is_64b = sys.maxsize > 2**32

ffi = FFI()
if is_64b: ffi.cdef("typedef long GoInt;\n")
else:      ffi.cdef("typedef int GoInt;\n")

ffi.cdef("""
typedef struct {
    void* data;
    GoInt len;
    GoInt cap;
} GoSlice;

typedef struct {
    const char *data;
    GoInt len;
} GoString;

GoInt Add(GoInt a, GoInt b);
double Cosine(double v);
void Sort(GoSlice values);
GoInt Log(GoString str);
""")

lib = ffi.dlopen("./awesome.so")

print("awesome.Add(12,99) = %d" % lib.Add(12,99))
print("awesome.Cosine(1) = %f" % lib.Cosine(1))

data = ffi.new("GoInt[]", [74,4,122,9,12])
nums = ffi.new("GoSlice*", {'data':data, 'len':5, 'cap':5})
lib.Sort(nums[0])
print("awesome.Sort(74,4,122,9,12) = %s" % [
    ffi.cast("GoInt*", nums.data)[i] 
    for i in range(nums.len)])

data = ffi.new("char[]", b"Hello Python!")
msg = ffi.new("GoString*", {'data':data, 'len':13})
print("log id %d" % lib.Log(msg[0]))

更多的语言demo请点击https://github.com/vladimirvivien/go-cshared-examples

添加新评论