孟斌

在本文中,我们将学习如何将 Go 代码编译成 C 动态库,并通过 C 程序调用 Go 动态库中的函数。我们将逐步介绍如何生成 Go 动态库,如何编写 C 程序来调用 Go 函数,以及如何在 C 程序中链接 Go 生成的共享库。

1. 目标

2. 环境准备

确保你的系统已经安装了以下工具:

3. 创建 Go 动态库

Go 提供了 -buildmode=c-shared 选项,允许我们将 Go 代码编译成 C 动态库(.so 文件)。

3.1 编写 Go 代码

首先,我们编写一个简单的 Go 代码文件,包含一个 Add 函数,用于返回两个整数的和。我们将通过 //export 注释来将 Add 函数导出给 C 程序。

go_functions.go:

// go_functions.go
package main

import "C"

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

在上面的 Go 代码中:

3.2 编译 Go 动态库

我们使用 go build -buildmode=c-shared 命令将 Go 代码编译为 C 动态库。该命令会生成 .so 文件(共享库文件)和 .h 头文件,供 C 程序使用。

$ go build -o libgo_functions.so -buildmode=c-shared go_functions.go

这条命令会生成两个文件:

4. 编写 C 程序调用 Go 动态库

现在,我们编写一个 C 程序,使用 #include 指令包含 Go 动态库的头文件,并调用其中的 Add 函数。

main.c:

#include <stdio.h>
#include "libgo_functions.h"  // 引入 Go 生成的头文件

int main() {
    // 调用 Go 动态库中的 Add 函数
    int result = Add(2, 3);
    
    // 打印返回值
    printf("Result of Add: %d\n", result);
    return 0;
}

在这个 C 程序中:

5. 编译并链接 C 程序

我们需要编译 C 程序并链接 Go 生成的共享库。使用 GCC 编译器时,需要指定 Go 共享库所在的路径,并通过 -L 选项告诉 GCC 去哪里查找库文件,使用 -l 选项指定库的名称。

编译命令如下:

$ gcc main.c -o main -L. -lgo_functions -pthread -ldl -Wl,-rpath=.

命令说明:

6. 运行 C 程序

完成编译后,运行 C 程序以调用 Go 动态库:

$ ./main
# 执行 C 程序后,输出将会显示 Go 中 `Add` 函数的返回结果:
Result of Add: 5

7. 注意事项


孟斯特

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特
—