簡體   English   中英

如何將Go函數作為參數傳遞給C函數?

[英]How to pass a Go function as an argument to a C function?

我正在嘗試將Go函數傳遞給C函數。

就像是:

stm := C.struct_tray_menu{
    ....
    fn: // definition of method
    ....
}
C.menu_cb(stm);

並將其傳遞給C函數:

static void menu_cb(struct tray_menu *item) {
  (void)item;
  printf("menu: clicked on %s\n", item->text);
}

我只想知道如何定義類似C.function的東西。

主要問題是對c中go定義的誤解。 所以最終的代碼看起來像


//export callOnMeGo
func callOnMeGo(in int) int {
    fmt.Printf("Go.callOnMeGo(): called with arg = %d\n", in)
    return  in+ 1
}

func main() {

    C.some_c_func((C.callback_fcn)(unsafe.Pointer(C.callOnMeGo_cgo)))
    //dont forget to use (funcDefinedInGO_cgo) for with postfix _cgo

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM