Error trying to call my C function #251
-
Dear purego community, package main
import (
"fmt"
"github.com/ebitengine/purego"
"unsafe"
)
type HelloFunc func()
func main() {
var libPath string
libPath = "./libmesa_ffi.so"
// Load the library
handle, err := purego.Dlopen(libPath, purego.RTLD_NOW)
if err != nil {
fmt.Print("Error loading library: %v\n", err)
return
}
defer purego.Dlclose(handle)
// Resolve the symbol for the function we want to call
helloSymbol, err := purego.Dlsym(handle, "helloworld")
if err != nil {
fmt.Print("Error resolving symbol: %v\n", err)
return
}
// Check if the symbol address is nil
if helloSymbol == 0 {
fmt.Println("Symbol address is null")
return
}
// Convert the symbol address to a function pointer
hello := *(*HelloFunc)(unsafe.Pointer(&helloSymbol))
// Call the function
hello()
} The code compiles but fails during runtime
Any idea of what could be wrong? thank you very much |
Beta Was this translation helpful? Give feedback.
Answered by
TotallyGamerJet
May 24, 2024
Replies: 1 comment
-
Most of the time you don't want to directly call Dlsym. The recommended way is to use RegisterLibFunc. Look at the examples folder: https://github.com/ebitengine/purego/blob/main/examples/libc/main.go |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
hajimehoshi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most of the time you don't want to directly call Dlsym. The recommended way is to use RegisterLibFunc. Look at the examples folder: https://github.com/ebitengine/purego/blob/main/examples/libc/main.go