Skip to content

Instantly share code, notes, and snippets.

@saranyan
Created January 31, 2012 14:08
Show Gist options
  • Save saranyan/1710663 to your computer and use it in GitHub Desktop.
Save saranyan/1710663 to your computer and use it in GitHub Desktop.
Testing Ruby FFI - Calling from C and Ruby
#include <stdio.h>
#include "mylibrary.h"
int main(){
char x[10];
double d = 10.0;
double *dp = &d;
double dd[3];
printf("tf0 - %lf\n",calculate_something(1,2.33));
printf("tf1 - %s\n", test_function_1("hello"));
printf("tf2 - %d\n", test_function_2(10.0,dp,x));
test_function_3();
test_function_4(dd);
printf("tf4 - %lf, %lf, %lf", dd[0], dd[1], dd[2]);
return 1;
}
require 'ffi'
require './mylibrary.rb'
objptr = FFI::MemoryPointer.new(:double).put_double(0, 10.0)
p objptr.get_double(0)
dd = FFI::MemoryPointer.new(:double,4, true)
c = MyLibrary.calculate_something(TESTVAR, 98.6) # note FFI handles literals just fine
d = MyLibrary.test_function_1("hello world")
e = MyLibrary.test_function_2(10.0,objptr,"hello")
MyLibrary.test_function_3
MyLibrary.test_function_4(dd)
puts "tf0 - #{c}"
puts "tf1 - #{d}"
puts "tf2 - #{e}"
arr = dd.get_array_of_double(0, 3)
puts "tf4 - #{arr}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment