I'm pretty new to more low level programming languages, so sorry if the answer is totally obvious.
For a project I need to import a shared library (sdk.so) and call its functions. With nm -D /lib/sdk.so --defined-only I can view the exported functions, so I know that they exist.
Now I want to call certain functions, so I load the library:
package main
// #cgo LDFLAGS: -ldl
// #include <stdlib.h>
// #include <dlfcn.h>
import "C"
import (
"errors"
"fmt"
"log"
"unsafe"
)
libName := "sdk"
libName := C.CString(name)
defer C.free(unsafe.Pointer(libName))
handle := C.dlopen(libName, C.RTLD_LAZY)
if handle == nil {
return nil, ErrSoNotFound
}
sym := C.CString("some_function")
defer C.free(unsafe.Pointer(sym))
someFunction := C.dlsym(handle, sym)
if mdpSdkAlloc == nil {
log.Fatal(ErrSoFunctionNotFound)
}
fmt.Println(someFunction)
But now I'm stuck and don't know how to call the function. The Println prints something like 0x7f50a5f9cec0, with an ampersand I'm getting something like 0xc0000b2018 and with an asterisk I'm getting "invalid indirect of someFunction (type unsafe.Pointer)". How can i cast this to a function call?
Thanks for any help.