This might also be relevant: https://github.com/ebitengine/purego

On Thursday, 15 May 2025 at 21:47:50 UTC+1 rudeus greyrat wrote:

> Thanks Jason, I was looking for something like that indeed :)
>
> However meanwhile I found an amazing Go package that does what I want 
> https://github.com/rainycape/dl
>
> He does the same as you do with some assembly and automatic type 
> conversion with reflect package to facilitate user experience :)
>
> Le jeudi 15 mai 2025 à 18:52:33 UTC+2, Jason E. Aten a écrit :
>
>> wow. that .go got mangled by copy and paste. 2nd attempt:
>>
>> package main
>>
>> /*
>> #cgo LDFLAGS: -ldl
>> #include <dlfcn.h>
>> #include <stdlib.h>
>> #include <stdio.h>
>>
>> // Define the function type that matches our C function
>> typedef int (*multiply_func)(int, int);
>>
>> // Helper function to load and call the multiply function
>> int call_multiply(int a, int b) {
>>     void* handle = dlopen("./libexample.so", RTLD_LAZY);
>>     if (!handle) {
>>         fprintf(stderr, "Error loading library: %s\n", dlerror());
>>         return -1;
>>     }
>>     // not C, but you'll need the moral equivalent of
>>     // defer dlclose(handle);
>>     // to clean up.
>>
>>     multiply_func multiply = (multiply_func)dlsym(handle, "multiply");
>>     if (!multiply) {
>>         fprintf(stderr, "Error finding function: %s\n", dlerror());
>>         return -1;
>>     }
>>
>>     return multiply(a, b);
>> }
>> */
>> import "C"
>> import "fmt"
>>
>> func main() {
>> // Call the C function through our wrapper
>> result := C.call_multiply(C.int(5), C.int(7))
>>
>> if result == -1 {
>> fmt.Println("Error calling multiply function")
>> return
>> }
>>
>> fmt.Printf("5 * 7 = %d\n", int(result))
>> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/247ec02a-96b1-4234-b328-ccb8b9e972dbn%40googlegroups.com.

Reply via email to