Search code examples
cgocgo

Calling C function from .so file by memory address from go module. (cgo)


I have an old .so file with a very complex clang parser in it and I have to call it from a go module.

    ...
    lib := C.dlopen(C.CString("./resources/bin/parser.so"), C.RTLD_LAZY)
    functions_address := C.dlsym(lib, C.CString("parse"))


Solution

  • I have solved this by defining a C typedef, creating a helper method and passing the "functions_address" to that helper method which calls the other function by reference

    typedef char (*parse) (char *file);
    
    char bridge (parse p, char* file) {
            p(file);
    }