Embedding Ruby in C code

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 tocos’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0xac): In function
math_sin': : undefined reference tosin’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0xe4): In function
math_tan': : undefined reference totan’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x11c): In function
math_acos': : undefined reference toacos’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x154): In function
math_asin': : undefined reference toasin’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x1bc): In function
math_exp': : undefined reference toexp’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x1f4): In function
math_log': : undefined reference tolog’
/usr/lib/ruby/1.6/i386-linux/libruby.a(math.o)(.text+0x22c): In function
math_log10': : undefined reference tolog10’
/usr/lib/ruby/1.6/i386-linux/libruby.a(numeric.o)(.text+0xe34): In
function flo_pow': : undefined reference topow’
/usr/lib/ruby/1.6/i386-linux/libruby.a(numeric.o)(.text+0x2446): In
function fix_pow': : undefined reference topow’
/usr/lib/ruby/1.6/i386-linux/libruby.a(string.o)(.text+0x524b): In
function rb_str_crypt': : undefined reference tocrypt’
/usr/lib/ruby/1.6/i386-linux/libruby.a(bignum.o)(.text+0x2395): In
function rb_big_pow': : undefined reference topow’
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0x7a): In function
dln_strerror': : undefined reference todlerror’
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0xb6): In function
dln_load': : undefined reference todlopen’
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0xc9): In function
dln_load': : undefined reference todlsym’
/usr/lib/ruby/1.6/i386-linux/libruby.a(dln.o)(.text+0x119): In function
dln_load': : undefined reference todlclose’
collect2: ld returned 1 exit status
make: *** [embeddedRuby] Error 1

···

==========================
SZYMON DREJEWICZ
drejewic@wsisiz.edu.pl

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

  #include "ruby.h"

  main() {
    ruby_init();

          ruby_init_loadpath(); /* to find the libraries */

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

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

  add -ldl -lcrypt -lm

Guy Decoux

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

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

ts wrote:

“S” == Szymon Drejewicz 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().

???

– [ 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

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

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

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?
:slight_smile:

···

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

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

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

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

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

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;

}

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

[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

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

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.

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