我们先看看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);
}
网友回复
如何破解绕开seedance2.0真人照片生成视频 限制?
python有哪些算法可以将视频中的每个帧图片去除指定区域水印合成新的视频?
iphone的激光雷达数据能否实时传输到three三维空间中?
豆包sora等ai视频生成大模型生成的视频水印如何去除?
python如何实现在电脑上拨号打电话给手机?
具身机器人与人形机器人区别?
nodejs如何将一个完整的js代码文件切割成不同的部分混淆后动态加载进入html运行?
为啥windows.onerror捕获js错误是这样的{"message":"Script error.","source":"","lineno":0,"colno":0,"stack":null,
2026年ai将全面接管编程?
WebMCP是干啥的?


