我们先看看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);
}
网友回复
同一个中英混合文本不同大模型计算tokens长度一致吗?
Browser Use / Playwright / Puppeteer 与Chrome DevTools Protocol(CDP)的关系?
能否在三维空间调用ai的api实现vrm模型执行任意的姿势动作与行走完成任务?
如何让openclaw小龙虾自动帮你打电话聊客户?
各大公司推出的claw是否是为了大家消费自己的大模型tokens?
云服务器什么配置才能部署openclaw?
为啥ai生成视频模型只能5秒10秒或15秒生成,不能一次生成1分钟1i小时呢?
技术上如何解决被曝光的ai投毒geo行为?
有没有哪个大模型可以根据声音和文字描述生成带声音的视频?
如何实现华为手机终端设备之间隔空握拳抓取传送信息?


