Search code examples
gocgo

How to reuse a Go callback in several packages from C?


Is there a way to build a Go + C application that:

  • From main package X, import packages Y and Z.
  • Package M exports a go callback F.
  • Packages X and Y are both built with accompanying C files, both want to call F from C source code.

Generally speaking I'm trying to figure out how to call a callback from accompanying C files in other modules which are used to build a final application. I coudn't figure out how to achieve this or something similar. I'm also interested in convoluted solutions.


Solution

  • I don't see a way to call a Go function across packages, but all cgo packages are linked into the same binary and can call each other. This means that you can export M.F to a C function in package M and call that C function from packages Y and Z.

    m/m.go:

    package m
    
    // void F();
    import "C"
    import "fmt"
    
    //export F
    func F() {
        fmt.Println("m.f")
    }
    

    m/m.h:

    void m_f();
    

    m/m.c:

    #include <stdio.h>
    #include "_cgo_export.h"
    #include "m.h"
    
    void m_f() {
        printf("m_f\n")
        F();
    }
    

    y/y.go:

    package y
    
    // The LDFLAGS lines below are needed to prevent linker errors
    // since not all packages are present while building intermediate
    // packages. The darwin build tag is used as a proxy for clang
    // versus gcc because there doesn't seem to be a better way
    // to detect this.
    
    // #cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
    // #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
    // #include "y.h"
    import "C"
    
    import (
        "fmt"
        _ "m"
    )
    
    func Y() {
        fmt.Println("y.Y")
        C.y()
    }
    

    y/y.h:

    void y();
    

    y/y.c:

    #include <stdio.h>
    #include "../m/m.h"
    
    void y() {
        printf("y.C.y\n");
        m_f();
    }