HOWTO create Ruby bindings for a library

Hello,

I have this library which I would like to use with Ruby. How do I do
it?

···


Giuseppe “Oblomov” Bilotta

“E la storia dell’umanità, babbo?”
“Ma niente: prima si fanno delle cazzate,
poi si studia che cazzate si sono fatte”
(Altan)
(“And what about the history of the human race, dad?”
“Oh, nothing special: first they make some foolish things,
then you study which foolish things have been made”)

Giuseppe

Ruby/DL
http://kt-www.jaist.ac.jp/~ttate/ruby/ruby-dl.html

-tom

···

On Mon, 2002-09-09 at 04:50, Giuseppe Bilotta wrote:

Hello,

I have this library which I would like to use with Ruby. How do I do
it?


Giuseppe “Oblomov” Bilotta

“E la storia dell’umanità, babbo?”
“Ma niente: prima si fanno delle cazzate,
poi si studia che cazzate si sono fatte”
(Altan)
(“And what about the history of the human race, dad?”
“Oh, nothing special: first they make some foolish things,
then you study which foolish things have been made”)


tom sawyer, aka transami
transami@transami.net

Giuseppe Bilotta wrote:

I have this library which I would like to use with Ruby. How do I do
it?

Tom has already mentioned Ruby/DL in a separate response. Two other
choices you might want to consider are Joel VanderWerf’s CGenerator
module (CGenerator) and SWIG
(http://www.swig.org). If it’s straight C code in the library I think
Ruby/DL might be a good choice and CGenerator possibly better; I haven’t
used either of them. If you’re dealing with C++ code, I think SWIG might
be the right choice.

I’m putting together a presentation on SWIG and Ruby for the Ruby
conference in November, and I’d like to (at least briefly) touch on some
of the alternatives (like Ruby/DL and CGenerator). I might also throw in
a few words about things that don’t actually exist yet for Ruby, but
which could be done (e.g. a Ruby equivalent of Boost::Python). If you
run across any others I’d like to hear about them.

Tom Sawyer wrote:

Giuseppe

Ruby/DL
http://kt-www.jaist.ac.jp/~ttate/ruby/ruby-dl.html

Thank you very much.

···


Giuseppe “Oblomov” Bilotta

“E la storia dell’umanità, babbo?”
“Ma niente: prima si fanno delle cazzate,
poi si studia che cazzate si sono fatte”
(Altan)
(“And what about the history of the human race, dad?”
“Oh, nothing special: first they make some foolish things,
then you study which foolish things have been made”)

Tom Sawyer wrote:

Ruby/DL
http://kt-www.jaist.ac.jp/~ttate/ruby/ruby-dl.html

I haven’t tried it yet, so I’m curious: how well does Ruby/DL deal with
C++ libraries? That is, can you construct C++ objects, call overloaded
functions, etc.?

Lyle Johnson wrote:

Giuseppe Bilotta wrote:

I have this library which I would like to use with Ruby. How do I do it?

Tom has already mentioned Ruby/DL in a separate response. Two other
choices you might want to consider are Joel VanderWerf’s CGenerator
module (CGenerator) and SWIG
(http://www.swig.org). If it’s straight C code in the library I think
Ruby/DL might be a good choice and CGenerator possibly better; I haven’t
used either of them. If you’re dealing with C++ code, I think SWIG might
be the right choice.

If you have the source, C or C++, I would try SWIG first. I haven’t used
it, but from what I understand, you won’t have to modify the library
much, just add some auxilliary interface description files.

CGenerator doesn’t really help much with an existing library, unless you
want to do a lot of cutting and pasting into your ruby program. If you
are developing a C library from scratch, or writing a ruby program that
needs to generate and execute C code on the fly (e.g., for math
expressions), then CGenerator is worth a look.

Probably not well, considering there’s no well-accepted standard for how
C++ compilers mangle names of member functions. If you correctly mangle
the name, and you happen to call the function using the correct calling
convention, and you look the function up properly in the vtable (if it
is a virtual function), and you happen to correctly catch any exception
that it throws and translate it into a Ruby exception, then you might be
off to a good start.

You’re probably better off using SWIG or wrapping the C++ code by hand.

Paul

···

On Mon, Sep 09, 2002 at 11:11:19PM +0900, Lyle Johnson wrote:

Tom Sawyer wrote:

Ruby/DL
http://kt-www.jaist.ac.jp/~ttate/ruby/ruby-dl.html

I haven’t tried it yet, so I’m curious: how well does Ruby/DL deal with
C++ libraries? That is, can you construct C++ objects, call overloaded
functions, etc.?

Joel VanderWerf wrote:

If you have the source, C or C++, I would try SWIG first. I haven’t used
it, but from what I understand, you won’t have to modify the library
much, just add some auxilliary interface description files.

Just to clarify: with SWIG you don’t need to modify the library at all.
You also don’t need the source code for the library (i.e. the include
files and the library alone are sufficient).