Embedding Ruby in C code

Paul Brannan pbrannan@atdesk.com wrote in message news: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

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

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

···

On Wed, Feb 26, 2003 at 03:54:44PM +0900, Edward Wilson wrote:

Paul Brannan pbrannan@atdesk.com wrote in message news: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

Thanks for baring with me and showing me my error.

I would suggest that you not assume systematically that things are broken :wink:

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

···


ed

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

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

···

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

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