# Embedding Ruby in C code

**URL:** <https://rubytalk.org/t/embedding-ruby-in-c-code/4535>\
**Category:** ruby-talk\
**Created:** [5 February 2003 12:26 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535 "2003-02-05T12:26:58Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Web2ed](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@Web2ed](https://rubytalk.org/u/Web2ed)\
**Post date:** [26 February 2003 06:54 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/21 "2003-02-26T06:54:44Z")

</div>

Paul Brannan [pbrannan@atdesk.com](mailto:pbrannan@atdesk.com) wrote in message news:[20030225151304.GZ22892@atdesk.com](mailto:20030225151304.GZ22892@atdesk.com)…

> > - I downloaded the 1.8 source straight from CVS
> > - and the following will not compile.
> 
> What error message do you get?
> 
> Paul

Thanks for asking:

I’m getting the following error:

`undefined reference to ruby\_init()’

My code again is:

#include “ruby.h”

int main(int argc, char \*\*argv)  
{  
ruby\_init();  
return 0;  
}

I have gotten past this several times with ruby 1.6.7 and others, this  
is the first time I have gotten this error. This is the first time I  
have built with ruby 1.8.

> **···**
>
> > On Tue, Feb 25, 2003 at 04:32:02PM +0900, Edward Wilson wrote:
> 
> –  
> ed

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [25 February 2003 20:51 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/22 "2003-02-25T20:51:30Z")

</div>

> > But what do you need the C files for? Everything they provide is in  
> > libruby.{so,a,dll?}.
> > 
> > And the only lib you have to take care of is libruby; the extensions  
> > are handled by Ruby and you don’t have to link against them. Unless you  
> > want to create a single executable with everything, I guess you could then  
> > build Ruby to use static linkage or utilize exerb on Windows.
> 
> Thanks for sticking with me on this thread.
> 
> I would like to embed ruby inside of a C application. I haven’t found  
> the examples in the few ruby books that exist to be helpful. I was  
> told to use rb\_protect() as a wrapper, then I discovered that eval.c  
> the source that defines `rb\_protect()’ isn’t included in the standard

batsman@tux-chan:~$ nm --dynamic /usr/lib/libruby.so | grep rb\_protect  
00021d1c T rb\_protect  
0000e048 T rb\_protect\_inspect

> binary RPMs, but is in the latest CVS sources. Then what I discovered  
> ^^^^^^ vs ^^^^^^^  
> was that the `make install` hack doesn’t install eval.c into the  
> normal /usr/loca/ruby/src directory as I would have expected it to.  
> That’s how we got so far into this thread.

‘make install’ is no hack, it belongs to the standard procedure  
./configure  
make  
make install

This generally doesn’t install the sources.

> All I want to do is "zooma, zoom zoom and a boom boom"err, start  
> hacking ruby at the C level without having to case loose files all  
> over the place. Building extensions was easy; I’m not having the same  
> luck embedding ruby however. The Ultimate magic would for someone to  
> post a code snippet that one could clone for their own project. The  
> examples in books have been foo bar so far.

Normally Ruby isn’t embedded but extended, so you won’t find many examples  
of the former. Another approach often used is wrapping legacy code with  
SWIG and for instance mapping C++ classes to Ruby, and then controlling  
everything from a Ruby script.

> **···**
>
> On Wed, Feb 26, 2003 at 05:33:58AM +0900, Edward Wilson wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> “You, sir, are nothing but a pathetically lame salesdroid!  
> I fart in your general direction!”  
> – Randseed on #Linux

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [26 February 2003 09:22 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/23 "2003-02-26T09:22:27Z")

</div>

If I’m right it compiles OK but you get the problem when linking  
(although it can be done in a single step…)

batsman@tux-chan:/tmp$ cat tst.c  
#include “ruby.h”

int main(int argc, char \*\*argv)  
{  
ruby\_init();  
return 0;  
}  
batsman@tux-chan:/tmp$ gcc -I /usr/lib/ruby/1.6/i386-linux/ -o tst tst.c  
/tmp/cc7Gey6B.o(.text+0x7): In function `main': : undefined reference to `ruby\_init’  
collect2: ld returned 1 exit status  
batsman@tux-chan:/tmp$ gcc -I /usr/lib/ruby/1.6/i386-linux/ -o tst tst.c -lruby  
batsman@tux-chan:/tmp$ ./tst  
batsman@tux-chan:/tmp$

[s/1.6/1.8/g above]

I would suggest that you not assume systematically that things are broken 😉

> **···**
>
> On Wed, Feb 26, 2003 at 03:54:44PM +0900, Edward Wilson wrote:
> 
> > Paul Brannan [pbrannan@atdesk.com](mailto:pbrannan@atdesk.com) wrote in message news:[20030225151304.GZ22892@atdesk.com](mailto:20030225151304.GZ22892@atdesk.com)…
> > 
> > > On Tue, Feb 25, 2003 at 04:32:02PM +0900, Edward Wilson wrote:
> > > 
> > > > - I downloaded the 1.8 source straight from CVS
> > > > - and the following will not compile.
> > > 
> > > What error message do you get?
> > > 
> > > Paul
> > 
> > Thanks for asking:
> > 
> > I’m getting the following error:
> > 
> > `undefined reference to ruby\_init()’
> > 
> > My code again is:
> > 
> > #include “ruby.h”
> > 
> > int main(int argc, char \*\*argv)  
> > {  
> > ruby\_init();  
> > return 0;  
> > }
> > 
> > I have gotten past this several times with ruby 1.6.7 and others, this  
> > is the first time I have gotten this error. This is the first time I  
> > have built with ruby 1.8.
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> We all know Linux is great… it does infinite loops in 5 seconds.  
> - Linus Torvalds about the superiority of Linux on the Amterdam Linux Symposium

---

<div class="post-metadata">

**Author:** ![Web2ed](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@Web2ed](https://rubytalk.org/u/Web2ed)\
**Post date:** [26 February 2003 17:56 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/24 "2003-02-26T17:56:13Z")

</div>

Thanks for baring with me and showing me my error.

> I would suggest that you not assume systematically that things are broken 😉

I apologize for assuming it was Ruby, thanks again for your help and patience.

> **···**
>
> –  
> ed

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [26 February 2003 22:10 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/25 "2003-02-26T22:10:27Z")

</div>

Do not accidentally assign me qualities I do not possess.  
I’m just a bad thread-killer 🙂

I’m normally _very_ patient, but wasn’t this time: I guess I got upset  
when I read that something I love could be considered sub-standard,  
sorry 😉

> **···**
>
> On Thu, Feb 27, 2003 at 02:56:13AM +0900, Edward Wilson wrote:
> 
> > Thanks for baring with me and showing me my error.
> > 
> > > I would suggest that you not assume systematically that things are broken 😉
> > 
> > I apologize for assuming it was Ruby, thanks again for your help and patience.
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> “Who is General Failure and why is he reading my hard disk?”  
> Microsoft spel chekar vor sail, worgs grate !!  
> – Felix von Leitner, [leitner@inf.fu-berlin.de](mailto:leitner@inf.fu-berlin.de)

[Previous page](https://rubytalk.org/t/embedding-ruby-in-c-code/4535.md?page=1)
