c - 如何调用作为参数接收的 C 函数

标签 c go cross-language

我想使用在 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++ 代码。

ma​​in.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/

相关文章:

C#、C++ 项目组合 : Could not load file or assembly

c++ - 我怎样才能让 CMake 用两种不同的语言编译同一个输入文件?

c++ - 找到唯一的毕达哥拉斯三元组

postgresql - 使用 GoLang 保存额外数据

c - 在 IDE 上完美运行时出现 "expected identified"错误

go - 是否可以使用 Google Classroom Go API 来评论事物?

go - 标准包窗口中的非标准导入

c# - 使用类名作为字符串将 python 类实例化为 C#

ios - 如果 sqlite3_prepare_v2() 返回一个错误代码,我该如何获取错误代码?

c - pthreads - 停止当前线程的执行并在特定事件后重新启动它们