Compiling a Ruby extension

Hello!

I am trying to manually compile a tiny Ruby extension, but finally run
into a problem. I wonder if anyone can give me a hint on what's wrong?

The code for the extension:

#include <stdio.h>
#include "ruby.h"
__declspec(dllexport) void Init_MyExt();
static VALUE initialize(VALUE self);
static VALUE perform(VALUE self);
VALUE cMyExt;
void Init_MyExt() {
  cMyExt = rb_define_class("MyExt", rb_cObject);
  rb_define_method(cMyExt, "initialize", initialize, 0);
  rb_define_method(cMyExt, "perform", perform, 0);
}
static VALUE initialize(VALUE self) {
  return self;
}
static VALUE perform(VALUE self) {
  puts("Here is the C code!");
  return self;
}

My setup: Windows XP, Ruby One-Click Installer 1.8.2-15, MinGW 3.4.2.

Now I compile the code above into a DLL:

gcc -c -IC:\ruby\lib\ruby\1.8\i386-mswin32 myext.c
gcc -shared -o MyExt.dll -Wl,-s myext.o C:\ruby\lib\msvcrt-ruby18.lib

Now I try to call this DLL via the following client:

require 'MyExt'
e = MyExt.new
e.perform

Unfortunatly, this results in the following error message:

../MyExt.dll: wrong argument type Fixnum (expected Class) (TypeError)
        from
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require'
        from C:/temp/rubyext/test.rb:1

Any ideas?

You can't use mingw to compile your extensions. You need to use the MS compiler. You can either get a hold of Visual Studio.Net or follow these instructios to cobble together the tools that MS release for free in about 5 different packages. http://www.rubygarden.org/ruby?WindowsCompiler

Axel wrote:

···

Hello!

I am trying to manually compile a tiny Ruby extension, but finally run
into a problem. I wonder if anyone can give me a hint on what's wrong?

The code for the extension:

#include <stdio.h>
#include "ruby.h"
__declspec(dllexport) void Init_MyExt();
static VALUE initialize(VALUE self);
static VALUE perform(VALUE self);
VALUE cMyExt;
void Init_MyExt() {
  cMyExt = rb_define_class("MyExt", rb_cObject);
  rb_define_method(cMyExt, "initialize", initialize, 0);
  rb_define_method(cMyExt, "perform", perform, 0);
}
static VALUE initialize(VALUE self) {
  return self;
}
static VALUE perform(VALUE self) {
  puts("Here is the C code!");
  return self;
}

My setup: Windows XP, Ruby One-Click Installer 1.8.2-15, MinGW 3.4.2.

Now I compile the code above into a DLL:

gcc -c -IC:\ruby\lib\ruby\1.8\i386-mswin32 myext.c
gcc -shared -o MyExt.dll -Wl,-s myext.o C:\ruby\lib\msvcrt-ruby18.lib

Now I try to call this DLL via the following client:

require 'MyExt'
e = MyExt.new
e.perform

Unfortunatly, this results in the following error message:

./MyExt.dll: wrong argument type Fixnum (expected Class) (TypeError)
        from
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require'
        from C:/temp/rubyext/test.rb:1

Any ideas?

Hello, Steve!

Thanks for answering so quickly!

You can't use mingw to compile your extensions. You need to use the MS
compiler.

What a pity! I guess this is due to the fact that Microsoft uses a
different executable format than MinGW and the One-Click Installer
happens to be compiled with Microsoft's compiler. I seem to remember
having seen a tool that converts object files between these formats.
Maybe I'll try that one before cobbling together a Microsoft
installation.

-------------- Axel <><

Here are the instructions for installing and using the free version of the
microsoft compiler:

Installing the free compiler from Microsoft

1. Download and install the SDK from
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/

2. Install the .NET redistributable package from http://tinyurl.com/95zt
3. Install the .NET SDK from http://tinyurl.com/97x7
4. Install the VC Toolkit from
Microsoft Learn: Build skills that open doors in your career

5. Copy the directories under 'C:\Program Files\Microsoft Visual C++
Toolkit 2003' to 'C:\Program Files\Microsoft Visual Studio .NET 2003\VC7',
overwriting all the files there. This is recommended because the VC Toolkit
does code optimization, while the .NET SDK does not.
6. Set your environment variables. Run 'C:\Program
Files\Microsoft.NET\SDK\V1.1\bin\sdkvars.bat'. Then run 'C:\Program
Files\Microsoft SDK\setenv.bat'. You can do this permanently through windows
or on a per session basis.

···

On 9/1/05, Axel <anieden@gmx.de> wrote:

Hello, Steve!

Thanks for answering so quickly!

> You can't use mingw to compile your extensions. You need to use the MS
> compiler.

What a pity! I guess this is due to the fact that Microsoft uses a
different executable format than MinGW and the One-Click Installer
happens to be compiled with Microsoft's compiler. I seem to remember
having seen a tool that converts object files between these formats.
Maybe I'll try that one before cobbling together a Microsoft
installation.

===============================
expanded tiny-urls:

2. http://tinyurl.com/95zt =
http://www.microsoft.com/downloads/details.aspx?familyid=262d25e3-f589-4842-8157-034d1e7cf3a3&displaylang=en

3. http://tinyurl.com/97x7
http://www.microsoft.com/downloads/details.aspx?FamilyId=9B3A2CA6-3647-4070-9F41-A333C6B9181D&displaylang=en

===============================