Search code examples
cgocross-language

How to call C function received as an argument


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


Solution

  • 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)