# Calling 'C' code from Ruby

**URL:** <https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234>\
**Category:** Ruby\
**Created:** [September 27, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234 "2009-09-27T17:51:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 27, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/1 "2009-09-27T17:51:48Z")

</div>

Hi all,

I am wondering if there is a tutorial that will show me how to call C  
code from  
Ruby?

I am planning on playing with the idea of adding encryption support for  
Ruby  
that will be faster than current (pure) native Ruby code plug-in out  
there  
called crypt. I may start with GnuPG to offer something to the  
community.

I am new to Ruby and if I can get this done, it will be my first  
submission to  
the Open Source community.

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)  
Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 27, 2009, 7:03pm UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/2 "2009-09-27T19:03:13Z")

</div>

Rajinder Y. wrote:

> Hi all,
> 
> I am wondering if there is a tutorial that will show me how to call C  
> code from Ruby?

One starting point is the README.EXT file in the ruby source. There’s  
also a chapter in the pickaxe book (Programming Ruby). As you get  
farther along you may want to look into swig, if you are trying to  
access an existing library.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 27, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/3 "2009-09-27T20:19:19Z")

</div>

Joel VanderWerf wrote:

> 

Thanks Joel,

I also came across di and ffi on my search as well and was wondering  
what is the  
most efficient technique? or what solution is the most cleanest way to  
make  
calls to C/C++ code from Ruby?

If anyone is calling their C/C++ code from Ruby, I would like to hear  
from them  
what limitations they may was discovered on the the various methods.

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)  
Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 27, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/4 "2009-09-27T23:55:54Z")

</div>

On Sep 27, 2009, at 11:18 AM, Rajinder Y. wrote:

> Thanks Joel,
> 
> I also came across di and ffi on my search as well and was wondering  
> what is the most efficient technique? or what solution is the most  
> cleanest way to make calls to C/C++ code from Ruby?
> 
> If anyone is calling their C/C++ code from Ruby, I would like to  
> hear from them what limitations they may was discovered on the the  
> various methods.

If you write your extension in C, it won’t work with JRuby. I find  
FFI / dl to be infuriatingly difficult to debug at times. Also when  
distributing a DL/FFI gem, you need to teach your users how to set  
LD\_LIBRARY\_PATH. Your extension performance will be faster if you  
write in C. With FFI/DL, it takes more time to call C functions.

I can go on, but my general rule of thumb is if you’re wrapping only a  
handful of functions, use FFI. If you’re wrapping a complex library,  
use C.

* * *

Aaron P.  
[http://tenderlovemaking.com](http://tenderlovemaking.com)

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 28, 2009, 4:32am UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/5 "2009-09-28T04:32:48Z")

</div>

Aaron P. wrote:

> If you write your extension in C, it won’t work with JRuby. I find FFI  
> / dl to be infuriatingly difficult to debug at times. Also when  
> distributing a DL/FFI gem, you need to teach your users how to set  
> LD\_LIBRARY\_PATH. Your extension performance will be faster if you write  
> in C. With FFI/DL, it takes more time to call C functions.
> 
> I can go on, but my general rule of thumb is if you’re wrapping only a  
> handful of functions, use FFI. If you’re wrapping a complex library,  
> use C.

Hi Aaron. thank you for the feedback. This is really good to know,  
definitely  
helpful in terms of the thing I want to avoid. Hopeful it’s not to  
difficult to  
get a “hello world” first step going =P

I guess I also want to evaluate/determine which technique is really  
efficient  
with passing large pools of data between C and Ruby.

> * * *
> 
> Aaron P.  
> [http://tenderlovemak](http://tenderlovemak)  
> –  
> Kind Regards,  
> Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)  
Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [September 28, 2009, 11:04am UTC](https://www.ruby-forum.com/t/calling-c-code-from-ruby/176234/6 "2009-09-28T11:04:58Z")

</div>

Dear Rajinder,

there’s also RubyInline  
([rubyinline | software projects | by ryan davis](http://www.zenspider.com/ZSS/Products/RubyInline/))  
and Rice ([http://rice.rubyforge.org/](http://rice.rubyforge.org/)) for bringing C and Ruby together.  
I can’t say whether they’d be better adapted for what you are trying to  
do,  
but I’d be interested to learn which of the different Ruby/C connection  
possibilities gives you a best result.

Best regards,

Axel
