//export NAME
as part of the comments which appear immediately before the function.
Your C code can then call the function under the specified name.
When any //export comments appear in the cgo input, cgo will create
two new files: _cgo_export.h and _cgo_export.c. The .h file declares
the Go functions in C syntax, and the intent is that your C code will
#include it in order to see the declarations that it can call. The
_cgo_export.c file contains the function wrappers; the file must be
compiled with gcc and linked with your C code.
There is an example in misc/cgo/life.
I implemented this as part of my ongoing work adding support for Go to
SWIG. Implementing SWIG's director feature requires the ability to
call back from C/C++ to Go, so I wrote that. I added support to cgo
in order to test the new feature.
Ian
So this means that one cannot simply pass a pointer to a Go function
to C? Based on this I wrote a test program that involved a wrapper
written in C (and using the new export functionality), that is passed
to a C function in another library, all of which is called via a
function defined in the comments above 'import "C"':
package callback
// #include "callgo.h"
// #include "funcwrap.h"
// void _call_go_add() {
// call_go(&wrap_GoAdd);
// }
import "C"
func Run() {
C._call_go_add()
}
//export GoAdd
func GoAdd(a, b int) int {
return a + b
}
Quite cumbersome. . . Will we be able to pass a Go function directly
in the future?
-Daniel
> On Apr 9, 3:45 pm, Ian Lance Taylor <i...@google.com> wrote:
>> When any //export comments appear in the cgo input, cgo will create
>> two new files: _cgo_export.h and _cgo_export.c. The .h file declares
>> the Go functions in C syntax, and the intent is that your C code will
>> #include it in order to see the declarations that it can call. The
>> _cgo_export.c file contains the function wrappers; the file must be
>> compiled with gcc and linked with your C code.
>
> So this means that one cannot simply pass a pointer to a Go function
> to C?
That is correct.
> Quite cumbersome. . . Will we be able to pass a Go function directly
> in the future?
6g/8g and gcc use different calling conventions and the functions run
on different stacks. That means that passing a function pointer
either from Go to C or from C to Go will always require wrapping at
some level. It's possible that cgo could be taught to create that
wrapping automatically. I don't plan to work on that myself, but it
could be an interesting project for somebody else.
Ian
Ostsol writes:> On Apr 9, 3:45 pm, Ian Lance Taylor wrote:
You can //export a Go function to be callable from C, and these can be used in callbacks that run from a go-owned thread. Be aware that these function calls can be something like 50x slower than a normal function call last time I benchmarked.
would this 50x be just for the callback?I am thinking of something like the comparison of the following:big_loop {data[i] = callback()}
versuscallback() {big_loop {data = eval()}}