C Extensions using MingW (invalid address LoadError)

Hi

I've had some real trouble getting my first C extensions for Ruby to link
correctly, and now they won't load into Ruby properly.

1) How to correctly compile and link extensions using the mingw gcc
compiler? It looks like extconf.rb is configured for MSVC, not gcc. So I
played with it and managed to at least get it to compile and link. But what
are the correct options? This is what I used:

gcc -mno-cygwin -DNT -D__MSVCRT__ -Os -IC:/bin/ruby/lib/ruby/1.8/i386-mswin3
2 \
        -IC:/Projects/Ruby/proj1/src/lib/c -I./.. -I./../missing \
        -c -TcRbColorUtils.c -o RbColorUtils.o RbColorUtils.c

gcc -Lc:/bin/ruby/lib -nostartfiles -mdll --target=mingw32 --export-all -de
f:RbColorUtils-i386-mswin32.def \
        -o RbColorUtils.so RbColorUtils.o rgbhsi.o msvcrt-ruby18.lib

2) Why doesn't my extension load? Calling it using "ruby -e "require
'./RbColorUtils'" gives the following error:
    ./RbColorUtils.so: 487: Attempt to access invalid address.
    - ./RbColorUtils.so (LoadError)
        from
c:/bin/ruby/lib/ruby/site_ruby/1.8/rubygems/loadpath_manager.rb:5:in
`require'
        from -e:1

3) Can I even do this? Can I produce a DLL with MingW/gcc and use it in the
standard Ruby Windows exe distro, which I assume is compiled with MSVC?

Any ideas would be greatly appreciated. There doesn't appear to be much
documentation on producing extensions except with MSVC.

Kian Wright

You might want to take a look at RubyInline. Experiment with simple stuff just to get a feel for it. When you want, run w/ ruby -d to see the commands and flags it is using to compile and link the inlined code.

Here is a simple example I wrote earlier today to help a friend out with their code. They were coding in ObjC (really just plain C at that stage) and having problems. Popping their example into inline made all compile and link issues just melt away. I should note that I do not develop on windoze and we have had some issues in the past with mingw, but those appear to be addressed and I haven't heard about anything similar in a while.

#!/usr/local/bin/ruby -w

require 'inline'

class Example

   inline(:C) do |builder|

     builder.c <<-'END'
     void myrand() {
       unsigned int i;
       srandom(0);
       for (i = 0; i < 100; i++) {
         printf("%.17Lf \t", (random() / (double) LONG_MAX ));
       }
       puts("");
     }
     END

   end

end

Example.new.myrand

···

On May 3, 2005, at 8:39 PM, Kian Wright wrote:

I've had some real trouble getting my first C extensions for Ruby to link
correctly, and now they won't load into Ruby properly.

1) How to correctly compile and link extensions using the mingw gcc
compiler? [...]

--
ryand-ruby@zenspider.com - http://blog.zenspider.com/
http://rubyforge.org/projects/parsetree/
http://rubyforge.org/projects/rubyinline/
Seattle.rb | Home

Kian Wright wrote:

[....]
Any ideas would be greatly appreciated. There doesn't appear to be much
documentation on producing extensions except with MSVC.

Do you have to use MinGW? I've been able to build an extension using
freely-downloadable MS tools. I (still) need to write up the steps, but
it worked.

Steve

Hi,

At Wed, 4 May 2005 12:39:47 +0900,
Kian Wright wrote in [ruby-talk:140979]:

1) How to correctly compile and link extensions using the mingw gcc
compiler? It looks like extconf.rb is configured for MSVC, not gcc. So I
played with it and managed to at least get it to compile and link. But what
are the correct options? This is what I used:

Does "configured for MSVC" mean that you use OneClick
Installer? It is linked against msvcr71.dll, which is
incompatible to msvcrt.dll used by mingw gcc.

···

--
Nobu Nakada

"Steven Jenkins" <steven.jenkins@ieee.org> wrote in message
news:4278D341.9020702@ieee.org...

Kian Wright wrote:
> [....]
> Any ideas would be greatly appreciated. There doesn't appear to be much
> documentation on producing extensions except with MSVC.

Do you have to use MinGW? I've been able to build an extension using
freely-downloadable MS tools. I (still) need to write up the steps, but
it worked.

I guess I don't... I was under the (apparently erroneous) assumption that
the MSVC compiler was $$. I will look for some free downloads of stuff
(maybe it's just in the .NET framework), and try it.

Was it fairly straightforward to compile and link; using mkmf as described
in the PickAxe?

KW

Kian Wright wrote:

Was it fairly straightforward to compile and link; using mkmf as described
in the PickAxe?

Yes. The hard part was figuring out (a) what to download, and (b) how to set up the environment so the compiler and linker could find everything they needed.

Download and install Visual C++ Toolkit, Platform SDK, and .NET Framework. One of these also had a prerequisite that required installing. Don't remember what it was, but it was obvious.

Look for .bat files that set up path and environment variables in the installed directories and execute them. The files I needed were called SetEnv.bat, vcvars32.bat, and sdkvars.bat. I'll try to write up the full paths tonight.

Append the necessary paths to %INCLUDE% and %LIB% to find headers and libraries for your extension.

With all that set up, the standard idiom using extconf.rb worked fine.

Steve

···

From my rough notes: