Include existing C/C++ libraries in Ruby

I want to use my existing C/C++ libraries in Ruby and is there a way to
do that? I tried SWIG but it doesn’t work out that good for complex
objects and methods that I have in my libraries. Any other ways to
accomplish this?

On Sat, 2006-08-05 at 02:57 +0900, Ritesh T. wrote:

I want to use my existing C/C++ libraries in Ruby and is there a way to
do that? I tried SWIG but it doesn’t work out that good for complex
objects and methods that I have in my libraries. Any other ways to
accomplish this?

I hand-wrap libraries. It’s not that hard, and I can micromanage the API
that way.

Aria

Aredridel wrote:

On Sat, 2006-08-05 at 02:57 +0900, Ritesh T. wrote:

I want to use my existing C/C++ libraries in Ruby and is there a way to
do that? I tried SWIG but it doesn’t work out that good for complex
objects and methods that I have in my libraries. Any other ways to
accomplish this?

I hand-wrap libraries. It’s not that hard, and I can micromanage the API
that way.

Aria

Hello Aria,

Can you demonstrate with some example. It will help me great in
understanding. Appreciate your help.

Ritesh.

On Sat, 5 Aug 2006, Ritesh T. wrote:

I want to use my existing C/C++ libraries in Ruby and is there a way to
do that? I tried SWIG but it doesn’t work out that good for complex
objects and methods that I have in my libraries. Any other ways to
accomplish this?

you can use them directly with ruby/dl

harp:~/build/ruby-1.8.4/ext/dl/sample > cat a.rb
require “dl/import”
require “dl/struct”

module LIBC
extend DL::Importable

 begin
   dlload "libc.so.6"
 rescue
   dlload "libc.so.5"
 end

 extern "int atoi(char*)"

end

p LIBC.atoi(“42”)

harp:~/build/ruby-1.8.4/ext/dl/sample > ruby a.rb
42

dl is included with ruby

-a

I have an idea, though I’ll honestly say that I don’t know if it’ll
work: try making some simpler C/C++ libraries, let SWIG do its thing
with those, and see what the code it produces looks like. It’s
basically a template or a reference!

Then again, I don’t work with Ruby extensions and C/C++ libraries
(though I intend to someday).

M.T.

unknown wrote:

On Sat, 5 Aug 2006, Ritesh T. wrote:

I want to use my existing C/C++ libraries in Ruby and is there a way to
do that? I tried SWIG but it doesn’t work out that good for complex
objects and methods that I have in my libraries. Any other ways to
accomplish this?

you can use them directly with ruby/dl

I used to use ruby/dl a lot, and it’s vastly better than the equivalent
stuff in Python, although the documentation is inadequate. But more
recently I’ve gotten in the habit of simply writing plain old Ruby
extensions (lots of good docs, and they are dead-easy) that wrap my
C/C++ code. I think that’s the best way, because you can keep all the
ugly stuff in C and your Ruby code stays nice and clean. You have to do
a certain amount of massaging between the C world and the Ruby world,
but it’s not hard. Read README.EXT in the ruby source distro.