I am trying to call a function from a shared object using ruby-ffi. I compiled the following into a shared object:
#include <stdio.h>
typedef struct _WHAT {
int d;
void * something;
} WHAT;
int doit(WHAT w) {
printf("%d\n", w.d);
return w.d;
}
The problem is, how do I declare the function with attach_function in Ruby? How is the struct argument (WHAT w) defined in the list of arguments in Ruby? It is not a :pointer, and does not seem to fit any of the other available types described in the ruby-ffi documentation, so what would it be?
Check how to use Structs in https://github.com/ffi/ffi/wiki/Structs, for your case:
Now attach the function, the argument, since you are passing the struct by value, is going to be
What.by_value(replacing What by whatever you have named you struct class above):And now how to call the function:
And now the complete file: