Created
February 13, 2021 08:10
-
-
Save jampajeen/97339c989c8073b76f73a8a95a5d860c to your computer and use it in GitHub Desktop.
Golang call C function in dynamic library using dlopen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cv | |
import "C" | |
import ( | |
"log" | |
"github.com/rainycape/dl" | |
) | |
// Capture ... | |
func Capture(input string, len int) (string, error) { | |
// TODO: this is a test function to Capture | |
lib, err := dl.Open("dist/clib/libci.so", 0) | |
if err != nil { | |
log.Fatalln(err) | |
return "", err | |
} | |
defer lib.Close() | |
var capture func(s *C.char, c C.int) *C.char | |
err = lib.Sym("capture", &capture) | |
if err != nil { | |
log.Fatalln(err) | |
return "", err | |
} | |
ret := capture(C.CString(input), C.int(len)) | |
return C.GoString(ret), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment