FAQ
Hi all,

I'm trying to call a Go function from C using CGo. The function shall
accept a char** argument.
The following code exemplifies what I'm trying to do:


package main

//
// typedef void (*Callback)(int argnum, char **args );
// void CallMe( Callback f ) {
// char *args[2] = { "Ciao", "mondo" };
// f(2, args);
//
//}
import "C"
import "unsafe"

func GoCallback( argnum C.int, args unsafe.Pointer ) {
// Convert args and do stuff
}


func main() {
C.CallMe(GoCallback)
}


I get the following error :

*Cannot use GoCallback (type func(_Ctype_int, unsafe.Pointer)) as type
*[0]byte in function argument*

How comes a C function pointer becomes a *[0]byte ( which should translate
to "pointer to a zero-length array of bytes" ) ??

P.S : Doing C.CallMe(C.Callback(GoCallback)) gives the following error,
which is less puzzling but does not explain much:

*cannot convert GoCallback (type func(_Ctype_int, unsafe.Pointer)) to type
C.Callback *


Thanks in advance for any help

Ciao
---
FB

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Minux at Jan 29, 2014 at 9:06 pm

    On Jan 29, 2014 9:27 AM, "Francesco Bochicchio" wrote:
    I'm trying to call a Go function from C using CGo. The function shall
    accept a char** argument.
    The following code exemplifies what I'm trying to do:


    package main

    //
    // typedef void (*Callback)(int argnum, char **args );
    // void CallMe( Callback f ) {
    // char *args[2] = { "Ciao", "mondo" };
    // f(2, args);
    //
    //}
    import "C"
    import "unsafe"

    func GoCallback( argnum C.int, args unsafe.Pointer ) {
    // Convert args and do stuff
    }


    func main() {
    C.CallMe(GoCallback)
    }


    I get the following error :

    Cannot use GoCallback (type func(_Ctype_int, unsafe.Pointer)) as type
    *[0]byte in function argument
    How comes a C function pointer becomes a *[0]byte ( which should
    translate to "pointer to a zero-length array of bytes" ) ??
    P.S : Doing C.CallMe(C.Callback(GoCallback)) gives the following error,
    which is less puzzling but does not explain much:
    cannot convert GoCallback (type func(_Ctype_int, unsafe.Pointer)) to type
    C.Callback
    you cannot pass a Go func to C function.

    what you can do is exporting a Go function, and let a C function call it.

    read golang.org/cmd/cgo for details.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Francesco Bochicchio at Jan 30, 2014 at 12:48 pm
    Yes, I forgot to add the //export directive ... which in turn forced me
    to put the C code in a separate file ... but at the
    end it seems to work.

    Thanks Again

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Jan 30, 2014 at 4:09 pm

    On Jan 30, 2014 7:48 AM, "Francesco Bochicchio" wrote:
    Yes, I forgot to add the //export directive ... which in turn forced me
    to put the C code in a separate file ... but at the
    end it seems to work.
    if your C code contains only tiny wrapper functions, you can still put in
    the same Go file and label them as static inline.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Francesco Bochicchio at Jan 30, 2014 at 12:51 pm
    Yes, I forgot to add the //export directive ... which in turn forced me
    to put the C code in a separate file ... but at the
    end it seems to work.

    Thanks Again

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Malcolm Smith at Jan 30, 2014 at 4:32 am
    Correct, you can't pass a Go func to a C function.

    But in cgo you get the same error if you pass a C function to a C function!

    C.Mix_HookMusicFinished(C.musicFinished)
    fails with "*cannot use _Cfpvar_fp_musicFinished (type unsafe.Pointer) as
    type *[0]byte in function argument*"

    while
    C.Mix_HookMusicFinished((*[0]byte)(C.musicFinished))
    works fine.

    So my question is also How come a C function pointer becomes a *[0]byte ?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Jan 30, 2014 at 4:09 pm

    On Jan 29, 2014 11:32 PM, "Malcolm Smith" wrote:
    Correct, you can't pass a Go func to a C function.

    But in cgo you get the same error if you pass a C function to a C function!
    C.Mix_HookMusicFinished(C.musicFinished)
    fails with "cannot use _Cfpvar_fp_musicFinished (type unsafe.Pointer) as
    type *[0]byte in function argument"
    while
    C.Mix_HookMusicFinished((*[0]byte)(C.musicFinished))
    works fine.

    So my question is also How come a C function pointer becomes a *[0]byte ?
    unsupported C pointer types are translated to *[0]byte.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Malcolm Smith at Jan 30, 2014 at 7:46 pm

    On Thursday, January 30, 2014 4:09:28 PM UTC, minux wrote:

    unsupported C pointer types are translated to *[0]byte.
    Right, so you are saying they are unsupported by Go, hence the need for the
    ugly (*[0]byte) type conversion. But as I am passing the C pointer type to
    a C function this doesn't matter and the code works.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Jan 30, 2014 at 8:49 pm

    On Jan 30, 2014 2:46 PM, "Malcolm Smith" wrote:
    On Thursday, January 30, 2014 4:09:28 PM UTC, minux wrote:
    unsupported C pointer types are translated to *[0]byte.
    Right, so you are saying they are unsupported by Go, hence the need for
    the ugly (*[0]byte) type conversion. But as I am passing the C pointer type
    to a C function this doesn't matter and the code works.
    if you cast the C function pointer to *[0]byte then it could be passed to a
    C function that takes a function pointer.

    the choice of *[0]byte is not arbitrary, it means the same as void * in C.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Malcolm Smith at Jan 30, 2014 at 9:52 pm

    On Thursday, January 30, 2014 8:48:55 PM UTC, minux wrote:

    the choice of *[0]byte is not arbitrary, it means the same as void * in C.
    Thanks, having to use (*[0]byte) was making me nervous, but if it means
    void * then it makes perfect sense.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 29, '14 at 2:27p
activeJan 30, '14 at 9:52p
posts10
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase