Wednesday, May 15, 2024
HomeGolangname shared object perform (loaded dynamically) from pointer in Go? - Getting...

name shared object perform (loaded dynamically) from pointer in Go? – Getting Assist


I attempt to name few shared object capabilities from Go. I don’t need to write a C commented code to construct a CGO interface for all capabilities.

I write my shared object like that:

#embrace <stdio.h>

void greet(char* identify) {
    printf("Good day, %s!n", identify);
}

I compile it with: gcc -shared -fPIC -o libgreet.so greet.c.

My Go code to name the greet perform:

bundle essential

// #cgo LDFLAGS: -ldl
// #embrace <dlfcn.h>
// #embrace <stdlib.h>
import "C"
import (
    "unsafe"
    "fmt"
)

func essential() {
    so_name := C.CString("./libgreet.so")
    defer C.free(unsafe.Pointer(so_name))
    function_name := C.CString("greet")
    defer C.free(unsafe.Pointer(function_name))

    library := C.dlopen(so_name, C.RTLD_LAZY)
    defer C.dlclose(library)
    perform := C.dlsym(library, function_name)

    greet := (*func(*C.char))(unsafe.Pointer(&perform))

    fmt.Println("%p %p %p %pn", greet, perform, unsafe.Pointer(perform), unsafe.Pointer(&perform))
    (*greet)(C.CString("Bob"))
}

When i begin the executable i get SIGSEGV errors.

I attempt to debug it with gdb and pointers printed appears to be good (that the identical worth than x greet in gdb). The segmentation fault come on the instruction 0x47dde4 <essential.essential+548> name r8 the place $r8 incorporates 0x10ec8348e5894855 (in all probability not a reminiscence tackle).

Do you might have any concept how i can repair this error ? There’s a soluce to name shared object perform in Go with no C code commented because the CGO syntax (i don’t discover any documentation to do it) ?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments