gpt4 book ai didi

go - 如何在go中使用golang c-shared library

转载 作者:行者123 更新时间:2023-12-05 01:52:55 25 4
gpt4 key购买 nike

我用 go 编写了 c-shared 库。

package main

import "C"
import "log"

//export RunLib
func RunLib() {
log.Println("Call RunLib")
}

func init() {
log.Println("Call init")
}

func main() {
log.Println("Call main")
}

我使用以下命令创建了库:go build -buildmode=c-shared -o lib.so lib.go

为了使用这个库,我写了这段 golang 代码。

package main

/*
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

static void callFromLib() {
void (*fn)();
void *h = dlopen("lib.so", RTLD_LAZY);
if (!h) {
fprintf(stderr, "Error: %s\n", dlerror());
return;
}

*(void**)(&fn) = dlsym(h, "RunLib");
if (!fn) {
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(h);
return;
}

fn();
dlclose(h);
}

*/
import "C"

func main() {
C.callFromLib()
}

如果我运行最后一个代码,它会抛出这个错误(go run call.go):

fatal error: bad sweepgen in refill

goroutine 1 [running, locked to thread]:
runtime.throw({0xb990782, 0xc000042a38})
/usr/local/go/src/runtime/panic.go:1198 +0x71 fp=0xc000042a18 sp=0xc0000429e8 pc=0xb930691
runtime.(*mcache).refill(0x41215b8, 0x2)
/usr/local/go/src/runtime/mcache.go:156 +0x24e fp=0xc000042a68 sp=0xc000042a18 pc=0xb91434e
runtime.(*mcache).nextFree(0x41215b8, 0x2)
/usr/local/go/src/runtime/malloc.go:880 +0x85 fp=0xc000042ab0 sp=0xc000042a68 pc=0xb90ba85
runtime.mallocgc(0x8, 0xb9ba300, 0x1)
/usr/local/go/src/runtime/malloc.go:1071 +0x4e8 fp=0xc000042b30 sp=0xc000042ab0 pc=0xb90c108
runtime.growslice(0xb9ba300, {0x0, 0x41a1910, 0x2}, 0xc00009c000)
/usr/local/go/src/runtime/slice.go:267 +0x4ea fp=0xc000042b98 sp=0xc000042b30 pc=0xb94586a
sync.(*Pool).pinSlow(0xba394e0)
/usr/local/go/src/sync/pool.go:223 +0x105 fp=0xc000042c30 sp=0xc000042b98 pc=0xb960ec5
sync.(*Pool).pin(0xba394e0)
/usr/local/go/src/sync/pool.go:206 +0x4e fp=0xc000042c48 sp=0xc000042c30 pc=0xb960d8e
sync.(*Pool).Get(0xba394e0)
/usr/local/go/src/sync/pool.go:128 +0x25 fp=0xc000042c80 sp=0xc000042c48 pc=0xb960ac5
fmt.newPrinter()
/usr/local/go/src/fmt/print.go:137 +0x25 fp=0xc000042ca8 sp=0xc000042c80 pc=0xb985d45
fmt.Sprintln({0xc000042d38, 0x1, 0x1})
/usr/local/go/src/fmt/print.go:280 +0x28 fp=0xc000042cf0 sp=0xc000042ca8 pc=0xb986008
log.Println({0xc000042d38, 0x24, 0x0})
/usr/local/go/src/log/log.go:329 +0x1e fp=0xc000042d20 sp=0xc000042cf0 pc=0xb98cd5e
main.RunLib(...)
/Users/.../demo/lib.go:8
_cgoexp_6b951f94a90e_RunLib(0xc000042d90)
_cgo_gotypes.go:36 +0x45 fp=0xc000042d58 sp=0xc000042d20 pc=0xb98cf85
runtime.cgocallbackg1(0xb98cf40, 0xc000042e60, 0x0)
/usr/local/go/src/runtime/cgocall.go:306 +0x29a fp=0xc000042e28 sp=0xc000042d58 pc=0xb903d1a
runtime.cgocallbackg(0xc0000001a0, 0x300000002, 0xc0000001a0)
/usr/local/go/src/runtime/cgocall.go:232 +0x109 fp=0xc000042eb8 sp=0xc000042e28 pc=0xb9039e9
runtime.cgocallbackg(0xb98cf40, 0x7ffeefbff737, 0x0)
<autogenerated>:1 +0x2f fp=0xc000042ee0 sp=0xc000042eb8 pc=0xb95e32f
runtime: unexpected return pc for runtime.cgocallback called from 0x4053e00
stack: frame={sp:0xc000042ee0, fp:0xc000042f08} stack=[0xc000042000,0xc000043000)
0x000000c000042de0: 0x000000c000042d9d 0x000000c000042e18
0x000000c000042df0: 0x000000000b95843b <runtime.exitsyscall+0x00000000000000fb> 0x000000c0000001a0
0x000000c000042e00: 0x000000c000042dd8 0x0000000000000000
0x000000c000042e10: 0x000000000b9c3dc0 0x000000c000042ea8
0x000000c000042e20: 0x000000000b9039e9 <runtime.cgocallbackg+0x0000000000000109> 0x000000000b98cf40 <_cgoexp_6b951f94a90e_RunLib+0x0000000000000000>
...
<_cgoexp_6b951f94a90e_RunLib+0x0000000000000000>
0x000000c000042ea0: 0x00007ffeefbff737 0x000000c000042ed0
0x000000c000042eb0: 0x000000000b95e32f <runtime.cgocallbackg+0x000000000000002f> 0x000000c0000001a0
...
<runtime.cgocallback+0x00000000000000b4>
0x000000c000042ee0: <0x000000000b98cf40 <_cgoexp_6b951f94a90e_RunLib+0x0000000000000000> 0x00007ffeefbff737
...
runtime.cgocallback(0x4004165, 0x4058340, 0xc000042f70)
/usr/local/go/src/runtime/asm_amd64.s:915 +0xb4 fp=0xc000042f08 sp=0xc000042ee0 pc=0xb95c134

goroutine 1 [runnable, locked to thread]:
unicode.init()
/usr/local/go/src/unicode/tables.go:9 +0x79
exit status 2

但是如果我使用 python 一切正常!

>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary("lib.so")
>>> 2022/03/02 01:08:17 Call init

>>> lib.RunLib()
2022/03/02 01:08:22 Call RunLib
0
>>>

信息

操作系统:macOs Big Sur 11.6.3 (20G415)

>>> clang --version
Apple clang version 13.0.0 (clang-1300.0.29.30)
>>> nm lib.so| grep RunLib
000000000008cfa0 T _RunLib
000000000008cf40 t __cgoexp_6b951f94a90e_RunLib
00000000000c4200 s __cgoexp_6b951f94a90e_RunLib.stkobj
>>> go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="~/Library/Caches/go-build"
GOENV="~/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="~/projects/go/.../go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wl/9rtfdx8x7jvgyn6t8hpq7yh00000gn/T/go-build401121298=/tmp/go-build -gno-record-gcc-switches -fno-common"

我用谷歌搜索并找到了这篇文章。但是文章中没有golang。

https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf

最佳答案

楼主描述的问题看起来很奇怪。我无法在 Linux 上重现它。

我的设置:Ubuntu 18、Go 1.19.2、gcc 7.5.0

我只做了一个改变:添加 pragma #cgo LDFLAGS: -ldlC 片段。 GCC 要求启用 dlopen & Co

package main

/*
#cgo LDFLAGS: -ldl
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

static void callFromLib() {
void (*fn)();
void *h = dlopen("lib.so", RTLD_LAZY);
if (!h) {
fprintf(stderr, "Error: %s\n", dlerror());
return;
}

*(void**)(&fn) = dlsym(h, "RunLib");
if (!fn) {
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(h);
return;
}

fn();
dlclose(h);
}

*/
import "C"

func main() {
C.callFromLib()
}

启动为:

LD_LIBRARY_PATH=/home/user/tmp/try-go/ go run call.go

LD_LIBRARY_PATH 变量指向带有lib.so的目录

输出:

2022/10/26 09:39:10 Call init
2022/10/26 09:39:10 Call RunLib

正如预期的那样,没有错误。

原始错误 bad sweepgen in refill 由垃圾收集器引发。对象的缓存变得不连贯。我怀疑问题在于程序中有两个运行时和两个垃圾收集器 - 一个来自调用程序,另一个来自加载的 lib.so。看起来两个垃圾收集器在同一个堆上运行时发生了冲突。

无论如何,在 go 1.19 中代码可以工作。我仍然不会通过 C dlopen 加载 Go 共享对象——谁知道两个 Go 运行时如何交互,尤其是当它们来自不同的编译器版本时。

关于go - 如何在go中使用golang c-shared library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71314253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com