Let’s start with the basic. What are bindings? It’s a way to call low level libraries (normally in C or C++) from another language (high level one, like ruby, java or whatever). This simple step requires two important things: 1) first, to know how we should call the method itself and 2) second, how to map the type from the high level language to C primitives types. To accomplish this, we have what is called foreign function interface which trivialize this task.
In ruby/jruby world we are gonna need ffi gem (jruby also has a compatible ffi gem), besides that the most important part is to have a clear interface in C. You can do bindings for a C++ library if you create a C interface first (because the bindings between C++ and C are free, the compiler knows how to do it by itself). So let’s cut the crap and write some code
First our C++ library which can be this few lines or thousands:
class awesome_object {
public:
awesome_object(int a, int b) : a_(a), b_(b) {}
int awesome_method() {
return a_ + b_;
}
protected:
int a_, b_;
} |
class awesome_object {
public:
awesome_object(int a, int b) : a_(a), b_(b) {}
int awesome_method() {
return a_ + b_;
}
protected:
int a_, b_;
}
Now the C bindings which will be our façade for our c++ library:
extern "C" {
typedef awesome_object_p void *;
awesome_object_p create_awesome_object(int a, int b) {
return static_cast<void *>(new awesome_object(a, b));
}
void delete_pointer_to_awesome_object(awesome_object_p p) {
delete static_cast<awesome_object *>(p);
}
int awesome_method(awesome_object_p p) {
awesome_object o = *static_cast<awesome_object *>(p);
return o.awesome_method();
}
} |
extern "C" {
typedef awesome_object_p void *;
awesome_object_p create_awesome_object(int a, int b) {
return static_cast<void *>(new awesome_object(a, b));
}
void delete_pointer_to_awesome_object(awesome_object_p p) {
delete static_cast<awesome_object *>(p);
}
int awesome_method(awesome_object_p p) {
awesome_object o = *static_cast<awesome_object *>(p);
return o.awesome_method();
}
}
Now ruby code that use ffi gem to be able to call the methods defined in C:
require 'ffi'
module AwesomeObject
include FFI
extend FFI::Library
ffi_lib "libawesome.so" # .dll if you are in windows, it doesn't matter the OS
attach_function "delete_pointer_to_awesome_object", "delete_pointer_to_awesome_object", [:pointer], :void
attach_function "awesome_method", "awesome_method", [:pointer], :int
attach_function "create_awesome_object", "create_awesome_object", [:int, :int], :pointer
end |
require 'ffi'
module AwesomeObject
include FFI
extend FFI::Library
ffi_lib "libawesome.so" # .dll if you are in windows, it doesn't matter the OS
attach_function "delete_pointer_to_awesome_object", "delete_pointer_to_awesome_object", [:pointer], :void
attach_function "awesome_method", "awesome_method", [:pointer], :int
attach_function "create_awesome_object", "create_awesome_object", [:int, :int], :pointer
end
With this you can use your C++ library from ruby code (I’d recommend package everything as a gem) just like this:
class MyTest
include AwesomeObject
def test!
object = create_awesome_object(11, 31)
awesome_method object
delete_pointer_to_awesome_object object
end
end
MyTest.new.test! |
class MyTest
include AwesomeObject
def test!
object = create_awesome_object(11, 31)
awesome_method object
delete_pointer_to_awesome_object object
end
end
MyTest.new.test!
So, if you have read until now, probably you just want something working, say no more. Download this tar.gz, and see all this by yourself. In the tar you have the code split into c/h cpp/hpp files as it should be, in the post I’ve put all together to simplify things. Just execute make test and if you have ruby, ffi gem and g++ installed on your system, you’ll see something like:
$ make test
g++ -g -Wall -fPIC -c awesome_object.cpp
g++ -g -Wall -fPIC -c awesome.c
g++ -g -Wall -fPIC -shared -o libawesome.so awesome.o awesome_object.o
ruby test.rb
42 |
$ make test
g++ -g -Wall -fPIC -c awesome_object.cpp
g++ -g -Wall -fPIC -c awesome.c
g++ -g -Wall -fPIC -shared -o libawesome.so awesome.o awesome_object.o
ruby test.rb
42