讨论数量:
先使用 github.com/spf13/cast
库来编写一个字符串转数字的函数 myatoi
。然后编译动态库,并使用 C 调用 myatoi
。
编写 Go 代码
main.go
package main
// 编译源码里必须有main package
import (
"C" // 源码必须import "C"
"github.com/spf13/cast"
)
// 导出符号位于main package,且前一行有//export NAME,注意// 与 export 中间不能有空格
//
//export myatoi
func myatoi(s string) int {
return cast.ToInt(s)
}
func main() {}
编译动态库
$ go build -buildmode=c-archive -o libconv.a main.go
编译后会在当前文件夹生成 libconv.a 与 libconv.h 文件
编写 C 代码使用动态库
main.c
#include <stdio.h>
#include "libconv.h"
int main(int argc, char const *argv[])
{
GoString s = {"123r", 3};// 有意输入字母 r
GoInt i = myatoi(s);
printf("call go atoi %lld\n", i);
return 0;
}
测试
$ cc main.c -o conv -L ./ -l conv && ./conv
# 输出:call go atoi 123
先使用
github.com/spf13/cast
库来编写一个字符串转数字的函数myatoi
。然后编译动态库,并使用 C 调用myatoi
。编写 Go 代码
main.go
编译动态库
编译后会在当前文件夹生成 libconv.a 与 libconv.h 文件
编写 C 代码使用动态库
main.c
测试