OT: pickaxe chap 17 and gcc (ruby/c)

I’m being thick here, but could someone help me out with
Pickaxe chapter 17?

I’m trying to get the first example to work
(can’t quote a page number as I’m reading it at work today online,
but the line:

“We can use the C version of the code in Ruby simply by require-ing it
dynamically at runtime (on most platforms).”

is confusing me. I assume you need to compile the c source to
a library - shared or otherwise? That step wasn’t mentioned though.

Does the following look like it’d work?
(I’m fairly sure it’s failing to require the file, but I
may as well see what’s wrong with it before I try to use it).

So far I’ve got to:

rasputin@shrike cruby$ gcc -c -o Test.o -I
/usr/local/lib/ruby/1.6/i386-freebsd4/ simple.c
rasputin@shrike cruby$ ls
Test.o simple.c test.rb
rasputin@shrike cruby$ file Test.o
Test.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (FreeBSD),
not stripped
rasputin@shrike cruby$ cat test.rb
#! /usr/local/bin/ruby -I.

require “Test” # this is wrong?
t = Test.new
t.add(“Dog food”, 3 , 4 , 5);

rasputin@shrike cruby$ ./test.rb
./test.rb:3:in `require’: No such file to load – Test (LoadError)
from ./test.rb:3

···


Rasputin :: Jack of All Trades - Master of Nuns

rasputin@shrike cruby$ gcc -c -o Test.o -I \
/usr/local/lib/ruby/1.6/i386-freebsd4/ simple.c

Well, the example is

svg% cat Test.c
#include <ruby.h>

static VALUE
t_init(VALUE self)
{
    rb_iv_set(self, "@arr", rb_ary_new());
    return self;
}

static VALUE
t_add(VALUE self, VALUE anObject)
{
    VALUE arr = rb_iv_get(self, "@arr");
    rb_ary_push(arr, anObject);
    return arr;
}

void Init_Test()
{
    VALUE cTest = rb_define_class("Test", rb_cObject);
    rb_define_method(cTest, "initialize", t_init, 0);
    rb_define_method(cTest, "add", t_add, 1);
}
svg%

The best after is to create a file extconf.rb

svg% cat extconf.rb
#!/usr/bin/ruby
require 'mkmf'
create_makefile('Test')
svg%

Then

svg% ruby extconf.rb
creating Makefile
svg%

svg% make
gcc -fPIC -g -O2 -I. -I/usr/local/lib/ruby/1.8/i686-linux -I/usr/local/lib/ruby/1.8/i686-linux -I. -c Test.c
gcc -shared -rdynamic -L"/usr/local/lib" -o Test.so Test.o -ldl -lcrypt -lm -lc
svg%

and finally you can use it :

svg% ruby -rTest -e 't = Test.new; t.add("Bill Chase"); p t'
#<Test:0x4009a0f0 @arr=["Bill Chase"]>
svg%

It's important that the name of the extension ('Test') is the same than
the name of the init function ('Init_Test')

Guy Decoux

You have to build it as a shared library (.so)

$ file /usr/local/lib/ruby/site_ruby/1.6/i386-freebsd4.7/fcgi.so
/usr/local/lib/ruby/site_ruby/1.6/i386-freebsd4.7/fcgi.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (FreeBSD), not stripped

Cheers,

Brian.

···

On Thu, May 22, 2003 at 08:56:18PM +0900, Rasputin wrote:

rasputin@shrike cruby$ gcc -c -o Test.o -I
/usr/local/lib/ruby/1.6/i386-freebsd4/ simple.c
rasputin@shrike cruby$ ls
Test.o simple.c test.rb
rasputin@shrike cruby$ file Test.o
Test.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (FreeBSD),
not stripped

rasputin@shrike cruby$ gcc -c -o Test.o -I
/usr/local/lib/ruby/1.6/i386-freebsd4/ simple.c

Well, the example is

svg% cat Test.c

yeah, got that bit.

The best after is to create a file extconf.rb

svg% cat extconf.rb
#!/usr/bin/ruby
require ‘mkmf’
create_makefile(‘Test’)
svg%

Then

svg% ruby extconf.rb
creating Makefile
svg%

Aah, right, that’s the steps I missed. Where did you get that from?
Is it documented anywhere?

and finally you can use it :

Thanks a lot, all working now.

···


Rasputin :: Jack of All Trades - Master of Nuns

http://www.rubycentral.com/book/ext_ruby.html

Creating an Extension

Having written the source code for an extension, we now need to
compile it so Ruby can use it. We can either do this as a shared
object, which is dynamically loaded at runtime, or statically link the
… etc

Cheers,

Brian.

···

On Thu, May 22, 2003 at 10:58:52PM +0900, Rasputin wrote:

The best after is to create a file extconf.rb

svg% cat extconf.rb
#!/usr/bin/ruby
require ‘mkmf’
create_makefile(‘Test’)
svg%

Then

svg% ruby extconf.rb
creating Makefile
svg%

Aah, right, that’s the steps I missed. Where did you get that from?
Is it documented anywhere?

···

On Thu, May 22, 2003 at 10:58:52PM +0900, Rasputin wrote:

The best after is to create a file extconf.rb

svg% ruby extconf.rb
creating Makefile
svg%

Aah, right, that’s the steps I missed. Where did you get that from?
Is it documented anywhere?

http://www.rubycentral.com/book/ext_ruby.html

Doh! Next time I’ll finish reading the chapter beofreasking questions
about it :slight_smile:


Rasputin :: Jack of All Trades - Master of Nuns