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