我们先看看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大模型能力排行榜有吗?
国产图片大模型seedream-5及qwen-image-3都无法生成360度全景图片?
听说OpenAI的Astra太强了可能会失控?
qwen3.8-max比较适合那些场景?
python有没有免费的股市行情数据api可用?
UUIDv7、nanoid、Snowflake、ULID与sparkid这些id生成器区别?
get请求报错NS_BINDING_ABORTED是什么原因?
happyhorse与seedance及minmax h3到底哪个更好?
scriptc为啥能生成一个不带nodejs的原生二进制可执行程序?
为啥huggingface那么多ai图片和视频模型都能一键脱衣没人管呢?


