# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Szymon\_Drejewicz1](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Szymon\_Drejewicz1](https://rubytalk.org/u/Szymon_Drejewicz1)\
**Post date:** [5 February 2003 12:26 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/1 "2003-02-05T12:26:58Z")

</div>

How to compile this file:

```
#include "ruby.h" 

main() {
  ruby_init();
  ruby_script("embedded");
  rb_load_file("embedded.rb");
  ruby_run();
  return 0;
}

```

Where ‘embedded.rb’ is:

```
#!/usr/bin/ruby
puts "BLA BLA"

```

?

I am asking because ‘  
gcc -o embeddedRuby embeddedRuby.o -O2 -fpic  
-I/usr/lib/ruby/1.6/i386-linux -rdynamic -L/usr/lib/ruby/1.6/i386-linux  
-lruby  
’ is not working properly, and I have this:

/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x74): In function  
`math_cos': : undefined reference to`cos’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0xac): In function  
`math_sin': : undefined reference to`sin’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0xe4): In function  
`math_tan': : undefined reference to`tan’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x11c): In function  
`math_acos': : undefined reference to`acos’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x154): In function  
`math_asin': : undefined reference to`asin’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x1bc): In function  
`math_exp': : undefined reference to`exp’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x1f4): In function  
`math_log': : undefined reference to`log’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x22c): In function  
`math_log10': : undefined reference to`log10’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(numeric.o)(.text+0xe34): In  
function `flo_pow': : undefined reference to`pow’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(numeric.o)(.text+0x2446): In  
function `fix_pow': : undefined reference to`pow’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(string.o)(.text+0x524b): In  
function `rb_str_crypt': : undefined reference to`crypt’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(bignum.o)(.text+0x2395): In  
function `rb_big_pow': : undefined reference to`pow’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0x7a): In function  
`dln_strerror': : undefined reference to`dlerror’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0xb6): In function  
`dln_load': : undefined reference to`dlopen’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0xc9): In function  
`dln_load': : undefined reference to`dlsym’  
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0x119): In function  
`dln_load': : undefined reference to`dlclose’  
collect2: ld returned 1 exit status  
make: \*\*\* [embeddedRuby] Error 1

> **···**
>
> ==========================  
> SZYMON DREJEWICZ  
> [drejewic@wsisiz.edu.pl](mailto:drejewic@wsisiz.edu.pl)

---

<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:** [5 February 2003 12:37 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/2 "2003-02-05T12:37:41Z")

</div>

gcc -o embeddedRuby embeddedRuby.o -O2 -fpic   
-I/usr/lib/ruby/1.6/i386-linux -rdynamic -L/usr/lib/ruby/1.6/i386-linux   
-lruby -lm -ldl

> **···**
>
> On Wed, Feb 05, 2003 at 09:26:58PM +0900, Szymon Drejewicz wrote:
> 
> > How to compile this file:
> > 
> > #include “ruby.h”
> > 
> > main() {  
> > ruby\_init();  
> > ruby\_script(“embedded”);  
> > rb\_load\_file(“embedded.rb”);  
> > ruby\_run();  
> > return 0;  
> > }
> > 
> > Where ‘embedded.rb’ is:
> > 
> > #!/usr/bin/ruby  
> > puts “BLA BLA”
> > 
> > ?
> > 
> > I am asking because ’  
> > gcc -o embeddedRuby embeddedRuby.o -O2 -fpic  
> > -I/usr/lib/ruby/1.6/i386-linux -rdynamic -L/usr/lib/ruby/1.6/i386-linux  
> > -lruby
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Let’s call it an accidental feature.  
> –Larry Wall

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [5 February 2003 12:44 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/3 "2003-02-05T12:44:07Z")

</div>

> &nbsp;&nbsp;#include "ruby.h"

> &nbsp;&nbsp;main() {  
> &nbsp;&nbsp;&nbsp;&nbsp;ruby\_init();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_init\_loadpath(); /\* to find the libraries \*/

> &nbsp;&nbsp;&nbsp;&nbsp;ruby\_script("embedded");  
> &nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_file("embedded.rb");  
> &nbsp;&nbsp;&nbsp;&nbsp;ruby\_run();  
> &nbsp;&nbsp;&nbsp;&nbsp;return 0;  
> &nbsp;&nbsp;}

> I am asking because '  
> gcc -o embeddedRuby embeddedRuby.o -O2 -fpic  
> -I/usr/lib/ruby/1.6/i386-linux -rdynamic -L/usr/lib/ruby/1.6/i386-linux  
> -lruby

&nbsp;&nbsp;add -ldl -lcrypt -lm

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Szymon\_Drejewicz1](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Szymon\_Drejewicz1](https://rubytalk.org/u/Szymon_Drejewicz1)\
**Post date:** [5 February 2003 14:47 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/4 "2003-02-05T14:47:29Z")

</div>

Where I can find more docs about embedded Ruby in C? For example, I know  
that function ‘rb\_protect()’ exists but I don’t have any manual.

I would like to use Ruby in C not by ruby\_run() because of 'exit()'  
calling. My main target is to make C program working like below:

‘a’ is some string;

while (cond) {  
change ‘a’ by ruby script;  
change ‘a’ by C;  
}

> **···**
>
> * * *
> 
> Szymon Drejewicz

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [5 February 2003 15:03 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/5 "2003-02-05T15:03:32Z")

</div>

> Where I can find more docs about embedded Ruby in C? For example, I know  
> that function 'rb\_protect()' exists but I don't have any manual.

rb\_protect() is described in the pickaxe (chapter "Ruby C Language API"),  
and also in eval.c

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Szymon\_Drejewicz1](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Szymon\_Drejewicz1](https://rubytalk.org/u/Szymon_Drejewicz1)\
**Post date:** [6 February 2003 09:50 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/6 "2003-02-06T09:50:18Z")

</div>

ts wrote:

> > > > > > “S” == Szymon Drejewicz [drejewic@wsisiz.edu.pl](mailto:drejewic@wsisiz.edu.pl) writes:
> 
> > Where I can find more docs about embedded Ruby in C? For example, I know  
> > that function ‘rb\_protect()’ exists but I don’t have any manual.
> 
> rb\_protect() is described in the pickaxe (chapter “Ruby C Language API”),  
> and also in eval.c
> 
> Guy Decoux

How to execute Ruby script without ruby\_run() after rb\_load\_file()  
command? I’m going to run some Ruby script many times in one C  
application, but direct reading ruby file and passing it to  
rb\_eval\_string() is not comfortable. In other hand ruby\_run() calls  
exit(). Maybe there is some way to launch script not by using ruby\_run().

???

---

<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:** [21 February 2003 07:40 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/7 "2003-02-21T07:40:47Z")

</div>

> – [Guy Decoux] wrote –  
> rb\_protect() is described in the pickaxe (chapter “Ruby C Language API”),  
> and also in eval.c

Does RedHat 8.0 hide eval.c, or worse, do they hack up their own  
version of Ruby? It’s great that Ruby comes with RedHat, but I wasn’t  
able to find `eval.c' until I pulled down a freash version from CVS. I greped through everything, `eval.c’ was only in a backed-up version  
of ruby I had from a previous OS. Has anyone else has this problem?

RedHat, I love them, but they have been switching things around  
lately. It’s best to rip out the version of Ruby that comes with the  
redhat installer and download yourself a fresh copy.

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

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [6 February 2003 10:08 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/8 "2003-02-06T10:08:15Z")

</div>

> How to execute Ruby script without ruby\_run() after rb\_load\_file()  
> command? I'm going to run some Ruby script many times in one C  
> application, but direct reading ruby file and passing it to  
> rb\_eval\_string() is not comfortable. In other hand ruby\_run() calls  
> exit(). Maybe there is some way to launch script not by using ruby\_run().

rb\_load\_protect() perhaps ???

it will call rb\_load\_file()

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [7 February 2003 02:39 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/9 "2003-02-07T02:39:47Z")

</div>

Hi,

> How to execute Ruby script without ruby\_run() after rb\_load\_file()  
> command? I’m going to run some Ruby script many times in one C  
> application, but direct reading ruby file and passing it to  
> rb\_eval\_string() is not comfortable. In other hand ruby\_run() calls  
> exit(). Maybe there is some way to launch script not by using  
> ruby\_run().

1.8 has ruby\_exec() and ruby\_cleanup(), but none in 1.6.  
ruby\_exec() executes loaded AST and returns tag status.  
Following example loads “embedded.rb” and runs it thrice.

#include “ruby.h”

int  
main(int argc, char \*\*argv)  
{  
int status;  
ruby\_init();  
ruby\_script(argv[0]);  
ruby\_init\_loadpath();  
rb\_protect((VALUE (\*)())rb\_load\_file, (VALUE)“embedded.rb”, &status);  
if (status == 0) {  
int i;  
for (i = 0; i \< 3; i++)  
if ((status = ruby\_exec()) != 0) break;  
}  
return ruby\_cleanup(status);  
}

> **···**
>
> At Thu, 6 Feb 2003 18:50:18 +0900, Szymon Drejewicz wrote:
> 
> –  
> Nobu Nakada

---

<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:** [21 February 2003 08:14 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/10 "2003-02-21T08:14:29Z")

</div>

Conspiracy theories sure are a lot of fun.

Do you think RH will do with Ruby and Perl what it did to Gnome and KDE?  
🙂

> **···**
>
> On Fri, Feb 21, 2003 at 04:40:47PM +0900, Edward Wilson wrote:
> 
> > > – [Guy Decoux] wrote –  
> > > rb\_protect() is described in the pickaxe (chapter “Ruby C Language API”),  
> > > and also in eval.c
> > 
> > Does RedHat 8.0 hide eval.c, or worse, do they hack up their own  
> > version of Ruby? It’s great that Ruby comes with RedHat, but I wasn’t  
> > able to find `eval.c' until I pulled down a freash version from CVS. I greped through everything, `eval.c’ was only in a backed-up version  
> > of ruby I had from a previous OS. Has anyone else has this problem?
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Linux: Where Don’t We Want To Go Today?  
> – Submitted by Pancrazio De Mauro, paraphrasing some well-known sales talk

---

<div class="post-metadata">

**Author:** ![Ian\_Macdonald](https://avatars.discourse-cdn.com/v4/letter/i/f19dbf/32.png) [@Ian\_Macdonald](https://rubytalk.org/u/Ian_Macdonald)\
**Post date:** [22 February 2003 04:02 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/11 "2003-02-22T04:02:56Z")

</div>

Nothing so nefarious.

eval.c is part of the source code, so it’s in the source RPM, not the  
binary RPM.

That said, there are other issues with the RPM, such as the packager’s  
decision to have libraries go into /usr/lib/site\_ruby, rather than  
/usr/lib/ruby/site\_ruby, which would make it analogous with the Perl and  
Python RPMs.

I’ve made my own RPMs, which you can get from:

```
http://www.caliban.org/files/redhat/

```

These are backwards-compatible with the Red Hat ones, but include extra  
niceties, such as readline completion for irb.

Ian

> **···**
>
> On Fri 21 Feb 2003 at 16:40:47 +0900, Edward Wilson wrote:
> 
> > > – [Guy Decoux] wrote –  
> > > rb\_protect() is described in the pickaxe (chapter “Ruby C Language API”),  
> > > and also in eval.c
> > 
> > Does RedHat 8.0 hide eval.c, or worse, do they hack up their own  
> > version of Ruby?
> 
> –  
> Ian Macdonald | Someone will try to honk your nose today.  
> [ian@caliban.org](mailto:ian@caliban.org) |  
> \>  
> \>  
> \>

---

<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:** [23 February 2003 18:47 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/12 "2003-02-23T18:47:44Z")

</div>

> That said, there are other issues with the RPM, such as the packager’s  
> decision to have libraries go into /usr/lib/site\_ruby, rather than  
> /usr/lib/ruby/site\_ruby, which would make it analogous with the Perl and  
> Python RPMs.

Everyone please bear with me; I’m still getting used to the Ruby file  
layout.

What is `site_ruby', and how does it differ from its peer 1.6? Both directories contain a lot of files, many of which seem to be duplicated. It seems extremely odd that `make install’ on Linux, and  
the installer on windows, doesn’t layout the files more cleanly. For  
example, on Linux why aren’t the .h files placed under the  
/usr/local/include directory and the .so files placed under the  
/usr/local/lib directory? To me, this is common sense. The files are  
laid out in the most unobvious way. Also, eval.c is part of the  
source distribution, but isn’t installed along with the rest of the  
files by the installer (`make install’ on Linux, and likewise on  
windows). Is the installer a work in progress, or is this by design?

It may seen like I’m nagging just to nag, but these are the small  
details that make a HUGE difference for those of us new to the  
language. I expect to find include files among the include files  
hopefully under a [/usr/local/include/ruby] subdirectory  
and the .so files among the other system wide .so files hopefully  
under a [/usr/local/lib/ruby] subdirectory.

The C API as well as the ease of finding and building applications  
with the .c, .h, .so, and lib (on windows) files is a big deal! Not  
finding all of these files where one expects them is annoying. These  
are those small details that make it easy to put off using Ruby for  
another year. When I install Ruby or any tool on my system, I expect  
all of the `include' and `library’ files to be neatly organized and  
easy to find. Once I run `make install’ I don’t want to have to  
download more files or manually move files aroundI want everything to  
be in its place ready to go. You guys are much smarter than I am, why  
is the Ruby file structure so obtuse?

> **···**
>
> –  
> 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:** [24 February 2003 09:56 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/13 "2003-02-24T09:56:45Z")

</div>

> > That said, there are other issues with the RPM, such as the packager’s  
> > decision to have libraries go into /usr/lib/site\_ruby, rather than  
> > /usr/lib/ruby/site\_ruby, which would make it analogous with the Perl and  
> > Python RPMs.
> 
> Everyone please bear with me; I’m still getting used to the Ruby file  
> layout.

[snip]

> The C API as well as the ease of finding and building applications  
> with the .c, .h, .so, and lib (on windows) files is a big deal! Not  
> finding all of these files where one expects them is annoying. These  
> are those small details that make it easy to put off using Ruby for  
> another year. When I install Ruby or any tool on my system, I expect  
> all of the `include' and `library’ files to be neatly organized and  
> easy to find. Once I run `make install’ I don’t want to have to  
> download more files or manually move files aroundI want everything to  
> be in its place ready to go. You guys are much smarter than I am, why  
> is the Ruby file structure so obtuse?

some things to consider:

- you don’t want to know where extensions’ .so files are. Period. You’re  
not linking against them, they’re loaded when you do ‘require’ or ‘load’  
inside Ruby, and it does know where they are.
- the only .so (dll) you might want to link against is libruby.so,  
available in /usr/lib/ or /usr/local/lib depending on your packaging
- as for .c files, why would you need them? Everything you need to link  
against is in libruby.so or libruby.a, so, why do you need the  
interpreter’s sources?
- extensions using ‘extconf.rb’ will use platform-tailored makefiles  
and know where everything is

Summing up, unless you have strange needs, you’ll find “everything to be  
in its place ready to go.”

> **···**
>
> On Mon, Feb 24, 2003 at 03:47:44AM +0900, Edward Wilson wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> I’ve no idea when Linus is going to release 2.0.24, but if he takes  
> too long Im going to release a 2.0.24unoff and he can sound off all  
> he likes.  
> – Alan Cox

---

<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:** [24 February 2003 22:10 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/14 "2003-02-24T22:10:56Z")

</div>

> some things to consider:
> 
> - you don’t want to know where extensions’ .so files are. Period. You’re  
> not linking against them, they’re loaded when you do ‘require’ or ‘load’  
> inside Ruby, and it does know where they are.
> - the only .so (dll) you might want to link against is libruby.so,  
> available in /usr/lib/ or /usr/local/lib depending on your packaging
> - as for .c files, why would you need them? Everything you need to link  
> against is in libruby.so or libruby.a, so, why do you need the  
> interpreter’s sources?
> - extensions using ‘extconf.rb’ will use platform-tailored makefiles  
> and know where everything is
> 
> Summing up, unless you have strange needs, you’ll find “everything to be  
> in its place ready to go.”

I don’t have any use for standalone Ruby. It’s when I can extend and  
or embed Ruby that I have infinite use for Ruby. So for me, the thing  
that matters most is where the C files and libs are; and that they are  
ALL there.

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

---

<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:** [25 February 2003 05:51 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/15 "2003-02-25T05:51:52Z")

</div>

> why would you need them? Everything you need to link  
> against is in libruby.so or libruby.a, so, why do you need the  
> interpreter’s sources?

How do you compile the following code snippet, what does your makefile look like?

#include “ruby.h”

VALUE do\_hello(VALUE arg)  
{  
rb\_eval\_string(“puts ‘hello world’”);  
return Qnil;  
}

int main(int argc, char \*\*argv)  
{  
int state;

```
ruby_init();
rb_protect(do_hello, Qnil, &state);
if (state) {
/* print error message */
}
return 0;

```

}

---

<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:** [25 February 2003 07:32 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/16 "2003-02-25T07:32:02Z")

</div>

/\* Okay, something is foo bar with my machine.

> **···**
>
> - 
> - I downloaded the 1.8 source straight from CVS
> - and the following will not compile.
> - 
> - I have gotten farther in the past; is
> - something wrong with the lastest version.
> - 
> 
> \*/
> 
> #include “ruby.h”
> 
> int main(int argc, char \*\*argv)  
> {  
> ruby\_init();  
> return 0;  
> }
> 
> /\* My make file is listed below \*/  
> all : rubyEmbed  
> gcc rubyEmbed.c -o rubyEmbed -I/home/zero/rubySource/ruby  
> -L/home/zero/rubySource/ruby -lruby-static

---

<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 12:54 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/17 "2003-02-25T12:54:10Z")

</div>

[snip]

Ok, the Makefile is kind of ugly with the RUBYDIR business but you can  
perhaps make it cleaner with ‘extconf.rb’ or something.

batsman@tux-chan:/tmp/re$ uname -mrs  
Linux 2.4.19-k7-preempt-lowlatency-via8235 i686  
batsman@tux-chan:/tmp/re$ cat Makefile  
RUBYDIR = $(shell ruby -e “require ‘rbconfig’; print Config::CONFIG[‘archdir’]”)  
CFLAGS = -I$(RUBYDIR)

tst: tst.o  
gcc -o $@ $^ -lruby  
batsman@tux-chan:/tmp/re$ cat tst.c  
#include \<ruby.h\>

VALUE do\_hello(VALUE arg)  
{  
rb\_eval\_string(“puts ‘hello world’”);  
return Qnil;  
}

int main(int argc, char \*\*argv)  
{  
int state;

```
 ruby_init();
 rb_protect(do_hello, Qnil, &state);
 if (state) {
   /* print error message */
 }
 return 0;

```

}

batsman@tux-chan:/tmp/re$ make  
cc -I/usr/lib/ruby/1.6/i386-linux -c -o tst.o tst.c  
gcc -o tst tst.o -lruby  
batsman@tux-chan:/tmp/re$ ./tst  
hello world

> **···**
>
> On Tue, Feb 25, 2003 at 02:51:52PM +0900, Edward Wilson wrote:
> 
> > > why would you need them? Everything you need to link  
> > > against is in libruby.so or libruby.a, so, why do you need the  
> > > interpreter’s sources?
> > 
> > How do you compile the following code snippet, what does your makefile look like?  
> > –  
> > \_ \_
> > 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> …Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and  
> the Ugly).  
> – Matt Welsh

---

<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 13:01 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/18 "2003-02-25T13:01:22Z")

</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.

> **···**
>
> On Tue, Feb 25, 2003 at 07:10:56AM +0900, Edward Wilson wrote:
> 
> > > some things to consider:
> > > 
> > > - you don’t want to know where extensions’ .so files are. Period. You’re  
> > > not linking against them, they’re loaded when you do ‘require’ or ‘load’  
> > > inside Ruby, and it does know where they are.
> > > - the only .so (dll) you might want to link against is libruby.so,  
> > > available in /usr/lib/ or /usr/local/lib depending on your packaging
> > > - as for .c files, why would you need them? Everything you need to link  
> > > against is in libruby.so or libruby.a, so, why do you need the  
> > > interpreter’s sources?
> > > - extensions using ‘extconf.rb’ will use platform-tailored makefiles  
> > > and know where everything is
> > > 
> > > Summing up, unless you have strange needs, you’ll find “everything to be  
> > > in its place ready to go.”
> > 
> > I don’t have any use for standalone Ruby. It’s when I can extend and  
> > or embed Ruby that I have infinite use for Ruby. So for me, the thing  
> > that matters most is where the C files and libs are; and that they are  
> > ALL there.
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> On a normal ascii line, the only safe condition to detect is a ‘BREAK’
> 
> - everything else having been assigned functions by Gnu EMACS.  
> – Tarl Neustaedter

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [25 February 2003 15:13 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/19 "2003-02-25T15:13:06Z")

</div>

What error message do you get?

Paul

> **···**
>
> 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.

---

<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:** [25 February 2003 20:33 UTC](https://rubytalk.org/t/embedding-ruby-in-c-code/4535/20 "2003-02-25T20:33:58Z")

</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 binary RPMs, but is in the latest CVS sources. Then what I discovered 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.

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.

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

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