我们先看看golang的buildmode命令解释
go help buildmode如何要编译成c的动态库的话就要使用buildmode=c-shared,新建一个文件libhello.go
The 'go build' and 'go install' commands take a -buildmode argument which
indicates which kind of object file is to be built. Currently supported values
are:
-buildmode=archive
Build the listed non-main packages into .a files. Packages named
main are ignored.
-buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable symbols will be those
functions exported using a cgo //export comment. Requires
exactly one main package to be listed.
-buildmode=c-shared
Build the listed main package, plus all packages it imports,
into a C shared library. The only callable symbols will
be those functions exported using a cgo //export comment.
Requires exactly one main package to be listed.
-buildmode=default
Listed main packages are built into executables and listed
non-main packages are built into .a files (the default
behavior).
-buildmode=shared
Combine all the listed non-main packages into a single shared
library that will be used when building with the -linkshared
option. Packages named main are ignored.
-buildmode=exe
Build the listed main packages and everything they import into
executables. Packages not named main are ignored.
-buildmode=pie
Build the listed main packages and everything they import into
position independent executables (PIE). Packages not named
main are ignored.
-buildmode=plugin
Build the listed main packages, plus all packages that they
import, into a Go plugin. Packages not named main are ignored.
package main注意,即使是要编译成动态库,也要有main函数,上面的import "C"一定要有 而且一定要有注释
import "C"
import "fmt"
//export Sum
func Sum(a int, b int) int {
return a + b
}
//export GetName
func GetName(firstName string) string{
return fmt.Sprint(firstName,"-so")
}
func main(){
}
go build -buildmode=c-shared -o libhello.so .\libhello.go
然后我们看c怎么调用
把libhello.so拷贝到/usr/lib中用于运行
新建一个文件夹hello_test ,把libhello.so libhello.h拷贝到文件夹hello_test中
把libhello.h中到GoString类型更改为_GoString
创建main.c,内容如下:
#include <stdio.h>编译命令如下:gcc main.c -o t1 -I./ -L./ -lhello
#include "libhello.h"
void main()
{
_GoString str;
str = GetName("bfw");
printf("%d\n",str.n);
}
网友回复
有没有免费让ai自动帮你接管操作电脑的mcp服务?
mcp为啥用Streamable HTTP 替代 HTTP + SSE?
scratchjr有没有开源的前端html网页版本源代码?
多模态大模型能否根据ui交互视频来来模仿写出前端交互动画效果ui代码?
如何用阿里云oss+函数计算fc+事件总线EventBridge+消息队列+数据库+redis缓存打造一个高并发弹性系统?
阿里云函数计算 FC如何在海外节点搭建一个代理网络?
ai studio中gemini build的代码如何发布到github pages等免费网页托管上 ?
如何在cursor、qoder、trae中使用Claude Skills功能?
有没有不用u盘就能重装系统的开源工具?
python如何固定摄像头实时计算停车场停车位剩余数量?


