# Embedded Ruby into C

**URL:** <https://www.ruby-forum.com/t/embedded-ruby-into-c/50011>\
**Category:** Ruby\
**Created:** [December 12, 2005, 3:18am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011 "2005-12-12T03:18:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 3:18am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/1 "2005-12-12T03:18:19Z")

</div>

Anyone got any good resources on embedding C into Ruby? The ones I  
found were a couple years old (i.e. for 1.6).

What I’d like to do is be able to call Ruby functions from the C code  
(don’t care about return value here), and be able to call C functions  
(and get the return data) from the Ruby code.

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 3:57am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/2 "2005-12-12T03:57:21Z")

</div>

On 12/11/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyone got any good resources on embedding C into Ruby? The ones I  
> found were a couple years old (i.e. for 1.6).

Chapter 21 in Programming Ruby, Second Edition covers it for Ruby 1.8.  
I haven’t tried it but it looks like a reasonably complete  
explanation.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 4:06am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/3 "2005-12-12T04:06:21Z")

</div>

On 12/11/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyone got any good resources on embedding C into Ruby? The ones I  
> found were a couple years old (i.e. for 1.6).
> 
> What I’d like to do is be able to call Ruby functions from the C code  
> (don’t care about return value here), and be able to call C functions  
> (and get the return data) from the Ruby code.

I created a Ruby extension in C called ‘functions.so’. My ruby script  
is require’ing it.

When I launch the Ruby script via ruby\_run() in C, I get ‘no such file  
to load – functions’ error.

Any ideas?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 4:12am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/4 "2005-12-12T04:12:22Z")

</div>

On 12/11/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I launch the Ruby script via ruby\_run() in C, I get ‘no such file  
> to load – functions’ error.

The source code from the book is here:  
[http://www.pragmaticprogrammer.com/titles/ruby/code/index.html](http://www.pragmaticprogrammer.com/titles/ruby/code/index.html)  
The examples from “Extending Ruby” may help.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 4:27am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/5 "2005-12-12T04:27:23Z")

</div>

On 12/11/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I launch the Ruby script via ruby\_run() in C, I get ‘no such file  
> to load – functions’ error.

Aha, calling ruby\_init\_loadpath() did the trick.

Essentially, I have a C program. I want to create a GUI (using  
ruby-gnome2) that can call the C program’s functions and process and  
report its data.

Originally, I added a small UDP server to the C program that would  
listen for UDP messages from the Ruby GUI and that’s how they  
exchanged information. The process of creating all the message  
formats for the UDP messages was a pain in the butt though. So I’m  
thinking of a different approach (embedding a Ruby program in the C  
executable and create Ruby extensions for calling the C functions).

Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 4:30am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/6 "2005-12-12T04:30:23Z")

</div>

On 12/11/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > is require’ing it.  
> > Originally, I added a small UDP server to the C program that would  
> > listen for UDP messages from the Ruby GUI and that’s how they  
> > exchanged information. The process of creating all the message  
> > formats for the UDP messages was a pain in the butt though. So I’m  
> > thinking of a different approach (embedding a Ruby program in the C  
> > executable and create Ruby extensions for calling the C functions).

Is this a reasonable approach:

int main()  
{  
initialize ruby stuff

create glib thread (or maybe pthread)  
{  
run ruby script that starts the GUI  
}  
}

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 6:03am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/7 "2005-12-12T06:03:42Z")

</div>

Anyone know how I can avoid the following compile errors?

/usr/lib/libruby.so: undefined reference to `gnu_dev_major@GLIBC_2.3.3' /usr/lib/libruby.so: undefined reference to`gnu\_dev\_minor@GLIBC\_2.3.3’

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 7:21am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/8 "2005-12-12T07:21:47Z")

</div>

One more question:

Can I embed a Ruby extension into a C executable, run a Ruby script  
from the C executable, and have the Ruby script have access to the  
Ruby extension that’s in the C executable?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 12, 2005, 9:28am UTC](https://www.ruby-forum.com/t/embedded-ruby-into-c/50011/9 "2005-12-12T09:28:10Z")

</div>

Whew, figured it all out.

My solution was to create a Ruby object in C that has access to all of  
the C program’s internal data, then create a normal Ruby object from a  
Ruby script file, then give that Ruby object the C/Ruby object that I  
first created.

So, now, in C, I have this gigantic array of structs. Each struct has  
a bunch of fields and a bunch of ‘sub structs’ (i.e. pointers to other  
structs).

What’s the easiest way to do reporting on this array of structs in Ruby?

Thanks,  
Joe
