From: ts Date: 2003-05-22T21:12:40+09:00 Subject: Re: OT: pickaxe chap 17 and gcc (ruby/c) >>>>> "R" == Rasputin writes: R> rasputin@shrike cruby$ gcc -c -o Test.o -I \ R> /usr/local/lib/ruby/1.6/i386-freebsd4/ simple.c Well, the example is svg% cat Test.c #include 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' # 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