dscrb00804 2019-08-31 06:21
浏览 79

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

I want to use Go library with some adjusted in C. I made GoAdder Go function that having 3 arguments int x, y and function typed f.

And GoAdder function going to call f argument.

adder.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
", x, y) 
    f(10); 

    return x + y
}

func main() {} // Required but ignored

And I built go package as a static library named libadder.a in above like this:

go build -buildmode=c-archive -o libadder.a adder.go

And then I have written C++ codes bellow.

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...
");
  int total = GoAdder(1, 7, &a);
  printf("C says: Go calculated our total as %i
", total);
  return 0;
}

I have complied the source like this:

gcc -pthread -o static_go_lib main.c adder/libadder.a

when executing codes above it occurs errors

unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x563c99b74244]

goroutine 17 [running, locked to thread]:
...

How to get a correct C function address a in go function GoAdder ?

I referenced https://github.com/draffensperger/go-interlang/tree/master/c_to_go/static_go_lib

  • 写回答

1条回答 默认 最新

  • dongyue5686 2019-09-01 04:14
    关注

    C function is just jump pointer, whereas golang's callback are complicate struct, and you cannot convert them. There's only one (safe) way to call C function pointer: 1) Declare that somewhere:

    //go:linkname cgocall runtime.cgocall
    //go:nosplit
    func cgocall(fn, arg unsafe.Pointer /* may be uintptr */) int32
    

    2) Also, be type safe:

    func GoAdder(x, y C.int, f unsafe.Pointer /*  don't sure if this available, mb C.uintptr_t */) C.int
    

    3) C function should take pointer (to what ever) as argument

    void a(GoInt *a)
    

    (I'd use native types)

    4)

    ten := 10
    cgocall(f, unsafe.Pointer(&ten))
    

    (It should be struct, if you wanna pass several args)

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!