From: Alan Moyer Date: 2002-01-02T04:04:48+09:00 Subject: [ruby-talk:29979] Extending Ruby on Windows (New Post) This is actually a continuation of the thread I stared on 12/28. I tightened up the problem I'm having so thought a new thread was in order. Problem: Not able to create a Ruby extension using mswin32 build of Ruby and MS VC++. I downloaded and installed the mswin32 binaries build of Ruby. The version I installed is 1.6.6. (From http://www.ruby-lang.org/~usa/mswin32/ruby-1.6.6-i586-mswin32.zip) I've been trying to learn how to wrote a Ruby extension and build it using MS VC++. I'm using version 6 of that program. I typed in the Test class extension that appears in the Programming Ruby book on page 173. I then created a DLL library. The project file I used had default settings. I linked with the library mswin32-ruby16.lib. The DLL built without error and I got the exported function Init_Test. The C language source code follows with some extra stuff I put in to debug: ----------------------------------------------------------------------- #include "ruby.h" #include __declspec(dllexport) void Init_Test(void); static VALUE t_init(VALUE self) { VALUE arr; arr = rb_ary_new(); rb_iv_set(self, "@arr", arr); return self; } static VALUE t_add(VALUE self, VALUE anObject) { VALUE arr; arr = rb_iv_get(self, "@arr"); rb_ary_push(arr, anObject); return arr; } VALUE cTest; void write(char* pStr); __declspec(dllexport) void Init_Test(void) { write("At A"); cTest = rb_define_class("Test", rb_cObject); write("At B"); rb_define_method(cTest, "initialize", t_init, 0); write("At C"); rb_define_method(cTest, "add", t_add, 1); write("At D"); } /* For debugging */ void write(char* pStr) { FILE *fp = fopen("test.log", "a"); fprintf(fp, pStr); fprintf(fp, "\n"); fclose(fp); } ----------------------------------------------------------------------- I then wrote the following Ruby program: ------------------------------------------------------------------------- require '/ruby_ms/test/Release/Test' ------------------------------------------------------------------------- where that's the location of the built DLL. When I try to run this program I get the following error: D:\Ruby_ms\alan>ruby test.rb test.rb:1: [BUG] Segmentation fault ruby 1.6.6 (2001-12-26) [i586-mswin32] From the debug output I see that the seg fault occurres within the call to define_class. (Execution got to "A" but not to "B".) What I've attempted seems about as basic as it gets. Am I doing something wrong or is there actually a bug in the Ruby build? I'd sure like to get beyond this point and will really appreciate your help. Thanks so much, Al