# Include existing C/C++ libraries in Ruby

**URL:** <https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419>\
**Category:** Ruby\
**Created:** [August 4, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419 "2006-08-04T19:57:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/1 "2006-08-04T19:57:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/2 "2006-08-04T20:00:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/3 "2006-08-04T20:03:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/4 "2006-08-04T21:19:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/5 "2006-08-04T20:51:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rtijoriwala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rtijoriwala](https://www.ruby-forum.com/u/rtijoriwala)\
**Post date:** [August 4, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/include-existing-c-c-libraries-in-ruby/101419/6 "2006-08-04T21:33:19Z")

</div>

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.
