我想使用在 C 语言中进行一些调整的 Go 库。 我制作了具有 3 个参数 int x、y 和函数类型 f 的 GoAdder Go 函数。
GoAdder 函数将调用 f 参数。
加法器.go
package main
import "fmt"
import "C"
//export Ftest
type Ftest func(C.int);
//export GoAdder
func GoAdder(x, y int, f Ftest) int {
fmt.Printf("Go says: adding %v and %v\n", x, y)
f(10);
return x + y
}
func main() {} // Required but ignored
我在上面将 go package 构建为一个名为 libadder.a 的静态库,如下所示:
go build -buildmode=c-archive -o libadder.a adder.go
然后我在下面编写了 C++ 代码。
main.c
#include <stdio.h>
#include "adder/libadder.h"
void a( GoInt a ){
printf("Hello %d", a);
}
int main() {
printf("C says: about to call Go...\n");
int total = GoAdder(1, 7, &a);
printf("C says: Go calculated our total as %i\n", total);
return 0;
}
我是这样编译源码的:
gcc -pthread -o static_go_lib main.c adder/libadder.a
执行上面的代码时出现错误
unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x563c99b74244]
goroutine 17 [running, locked to thread]:
...
如何在go函数GoAdder中获取正确的C函数地址a?
我引用了 https://github.com/draffensperger/go-interlang/tree/master/c_to_go/static_go_lib
最佳答案
C函数只是跳转指针,而golang的回调是复杂的struct,不能转换。 只有一种(安全的)方法可以调用 C 函数指针: 1)在某处声明:
//go:linkname cgocall runtime.cgocall
//go:nosplit
func cgocall(fn, arg unsafe.Pointer /* may be uintptr */) int32
2) 此外,类型安全:
func GoAdder(x, y C.int, f unsafe.Pointer /* don't sure if this available, mb C.uintptr_t */) C.int
3) C 函数应该将指针(指向任何东西)作为参数
void a(GoInt *a)
(我会使用原生类型)
4)
ten := 10
cgocall(f, unsafe.Pointer(&ten))
(它应该是结构,如果你想传递多个参数)
关于c - 如何调用作为参数接收的 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57735683/