package main
/*
int Add(int i) {
return i + 1;
}
*/
import "C"
import "fmt"
func main() {
}
//export Foo
func Foo() {
fmt.Printf("Foo!\n")
C.Add(1)
}
$ go build -buildmode=c-archive mde.go
# command-line-arguments
/tmp/go-build123475493/command-line-arguments/_obj/mde.cgo2.o: In function `Add':
./mde.go:5: multiple definition of `Add'
/tmp/go-build123475493/command-line-arguments/_obj/_cgo_export.o:../mde.go:5: first defined here
collect2: error: ld returned 1 exit status
I’m trying to implement a way to register a c-function as a callback:
package main
// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
// return f();
// }
//
// int fortytwo()
// {
// return 42;
// }
import "C"
import "fmt"
import "unsafe"
var handler C.intFunc
// export SetCallback
func SetCallback(f unsafe.Pointer) {
handler = c.intFunc(f)
}
// export Call
func Call() {
// here do something in Go, like starting a http server, run the registered callback in a request handler and send back the result in response
c.bridge_int_func(fh);
}
func main() {
}
compile the code above into a c-archive file, then in the c-program I will call SetCallback
to register callback and then Call
to start a server.
But as I say, the go file can’t be compiled with -buildmode=c-archive
.
Any workaround about this? My main program must be written in C, don’t suggest me to change this please, Thanks.