Ruby c extensions on windows

P

Philliam Auriemma

Hey guys,
I have a ruby extension I have written in c++, and I am using Windows.
My code successfully compiles to a .dll, and I put it in the folder
where all the other compiled extensions for ruby are (they are
included with ruby, like socket.so, etc), and requiring it in ruby
does not work. Is this because ruby does not work with dlls, and you
have to have a .so file? If so, how can I turn my code into a .so?
 
L

Luis Lavena

Hey guys,
I have a ruby extension I have written in c++, and I am using Windows.
My code successfully compiles to a .dll, and I put it in the folder
where all the other compiled extensions for ruby are (they are
included with ruby, like socket.so, etc), and requiring it in ruby
does not work. Is this because ruby does not work with dlls, and you
have to have a .so file? If so, how can I turn my code into a .so?

Ruby does load extensions from DLLs, at least all the versions I'm
aware of.

What is the exact error you're getting?

If is "unable to find Init_my_extension" is because your DLL is not
exporting the proper symbols.

Please read README.EXT that comes with Ruby source code, or refer to
Programming Ruby about creating a C extension.
 
P

Philliam Auriemma

I cannot remember the exact error text, but it is the same error that
results if you try to "require 'asdf'". It just says the thing
couldn't be found. Any naming conventions I have to follow? I hope I
can get this working without having to make a .so.
 
L

Luis Lavena

I cannot remember the exact error text, but it is the same error that
results if you try to "require 'asdf'". It just says the thing
couldn't be found. Any naming conventions I have to follow? I hope I
can get this working without having to make a .so.

First: avoid naming your extension with exact name of the DLL you're
trying to link or provide bindings.

One example is naming the extension "zlib" when there is a zlib.dll in
the PATH that is not a Ruby C extension.

A good introductory reading will be this posts:

http://tenderlovemaking.com/2009/12/18/writing-ruby-c-extensions-part-1/

Since Ruby own README.EXT is very complicated (but more complete)

HTH,
 
N

Nilbert Nullingsworth

I cannot follow this tutorial for some reason. I have no idea what to
do after I've got the files set up, make is not a recognized command,
and trying to do the rake leads to about three errors like this:

error loading "C:/Ruby/lib/ruby/gems/1.9.1/gems/hoe-2.5.0/lib/hoe/
rubyforge.rb":
182: The operating system cannot run %1. - C:/Ruby/lib/ruby/1.9.1/
i386-mswin3
2/openssl.so. skipping...

and then fails with 'rake aborted!'.

I also cannot find the mt.exe that other tutorials say to use, even
though I have Visual Studio Express 2008 installed, along with the
windows sdk and all it's components.

Why is writing a ruby extension so hard?
 
R

Roger Pack

Philliam said:
Hey guys,
I have a ruby extension I have written in c++, and I am using Windows.
My code successfully compiles to a .dll, and I put it in the folder
where all the other compiled extensions for ruby are (they are
included with ruby, like socket.so, etc), and requiring it in ruby
does not work. Is this because ruby does not work with dlls, and you
have to have a .so file? If so, how can I turn my code into a .so?

I'm not sure about VC land, but if you use mingw + devkit (and mkmf) it
should compile a .so file for you.

I'm not positive, but I heard that 1.8 *can* load .dll files, but 1.9
requires .so

I wish I could help you more but I haven't had much work with MSVC.

-r
 
L

Luis Lavena

I cannot follow this tutorial for some reason. I have no idea what to
do after I've got the files set up, make is not a recognized command,
and trying to do the rake leads to about three errors like this:

error loading "C:/Ruby/lib/ruby/gems/1.9.1/gems/hoe-2.5.0/lib/hoe/
rubyforge.rb":
 182: The operating system cannot run %1.   - C:/Ruby/lib/ruby/1.9.1/
i386-mswin3
2/openssl.so. skipping...

and then fails with 'rake aborted!'.

I also cannot find the mt.exe that other tutorials say to use, even
though I have Visual Studio Express 2008 installed, along with the
windows sdk and all it's components.

Why is writing a ruby extension so hard?

Because you're using the wrong tools, and because when you get "RTFM"
there should be a better manual in the first place, which something
Ruby lacks.

First: unless you compiled Ruby 1.9.1 with the version of Visual
Studio you're using, it will not compile the extensions. Compilers are
not exchangeable in Ruby.

Second: The version and setup should be pretty much exactly the same,
if not, it will fail.

Third: A recommendation:

Download and install RubyInstaller 1.9.1 or 1.8.6:

http://rubyinstaller.org/

Then download and install the Development Kit, which includes MinGW
+MSYS to make extension compilation simple.

Follow this tutorial to get those installed:

http://geeksharp.com/2010/01/18/windows-ruby-native-gems-1-9-1/

Then make and everything will work.

Fourth: be polite.

We all do our best to help you, please breathe first before insulting
or use improper words to people is trying to help you.

Fifth: Python has the same sort of issues mixing and matching
compilers. Good luck.
 
N

Nilbert Nullingsworth

Actually it is not the tools, it is my lack of knowledge on how to use
them. First of all, it would never compile because it would find some
reason not to, such as missing a header file (like winsock2.h, which
Visual C++ is not missing, even though I set the compiler to look for
includes in the VC++ include directory as well, even though winsock2.h
doesn't even exist in that folder... So that's weird that VC++ can do
it). Or it will be missing sys/time.h, even though it's not supposed
to look for time.h in sys/. Then it can't find pcap.h even though it's
in both the VC++ include directory which I have told the tools to
search for include files, as well as in the same directory as the C++
file, and the same directory from which the compiler was started from
the command line. Visual C++ compiles it with no problem but that is
because I have set up the program in the options to get the correct
libs and include files and command line options, something which, even
after multiple hours of searching with google. I do not know how to
do. Along with several other errors like not being able to use nmake
or rake or one of the other tools because openssl.so is messing up as
the operating system cannot run %1 (what the blazes).

Pardon my outburst, I am severely frustrated because I have been at
this constantly for about two days, and the multitude of travesties
that have befallen me is very discouraging. I will look at that dev
kit you suggested and be back for more help.
-Nilbert
 
L

Luis Lavena

Actually it is not the tools, it is my lack of knowledge on how to use
them. First of all, it would never compile because it would find some
reason not to, such as missing a header file (like winsock2.h, which
Visual C++ is not missing, even though I set the compiler to look for
includes in the VC++ include directory as well, even though winsock2.h
doesn't even exist in that folder... So that's weird that VC++ can do
it). Or it will be missing sys/time.h, even though it's not supposed
to look for time.h in sys/. Then it can't find pcap.h even though it's
in both the VC++ include directory which I have told the tools to
search for include files, as well as in the same directory as the C++
file, and the same directory from which the compiler was started from
the command line. Visual C++ compiles it with no problem but that is
because I have set up the program in the options to get the correct
libs and include files and command line options, something which, even
after multiple hours of searching with google. I do not know how to
do. Along with several other errors like not being able to use nmake
or rake or one of the other tools because openssl.so is messing up as
the operating system cannot run %1 (what the blazes).

First, and again. Ruby is very picky about compilers and its version.

You should use exactly the same version of the compiler that was used
to create that Ruby binary.

If you're using either garbagecollect downloads or One-Click
Installer, then you need Visual C 6.0

That can't be sent, shared or adquired, since is 12 years old
compiler.

Please, again, try the above indicated instructions about getting
RubyInstaller and the Development Kit.

Once you have that setup, you can workout all the other details.

Help us help you, you actually don't provide the real information we
need. For example, you mention "It can find pcap.h which I have told
the tools to search for include files"

How you did that? We don't look for narrative text, we want facts and
screen output, actual error messages and environment variables.
Pardon my outburst, I am severely frustrated because I have been at
this constantly for about two days, and the multitude of travesties
that have befallen me is very discouraging. I will look at that dev
kit you suggested and be back for more help.


Is RubyInstaller AND the DevKit.

Also, when reporting issues, again, try to provide all the information
relevant to your environment:

Ruby version (ruby -v)
RubyGems environment info (gem env)
Your PATH (set PATH)
Your OS and configuration (which flavor of Windows, 32 or 64 bits,
etc.)

Complete output of the error you're receiving. If is long, use a paste
service like Pastie or Gist (GitHub) (if you have an account).

Also, if you can provide the sample script that generated the issue,
the better.

Again, help us help you.
 
R

Ralph Shnelvar

Friday, January 22, 2010, 4:25:08 PM, you wrote:

LL> Again, help us help you.

Like Nilbert Nullingsworth, I am going to want to build a C extension.



I am running Instant Rails.

f:\Ralph-Rails-Apps>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

f:\Ralph-Rails-Apps>rails -v
Rails 2.3.5

f:\Ralph-Rails-Apps>gem -v
1.3.5


How can I tell what the underlying compiler to Ruby is/was since I want to have extensions that will run in this environment?

Can Instant rails use Ruby 1.9?



Is there a step-by-step written anywhere about how to write an extension in the Windows environment?
 
R

Roger Pack

How can I tell what the underlying compiler to Ruby is/was since I want
to have extensions that will run in this environment?

one possibility:
=> #<Gem::platform:0xdb1700 @cpu="x86", @os="mingw32", @version=nil>

(the mingw32 means it was build using mingw gcc -- I "think" that
instant rails used MSVC 6.0)
Can Instant rails use Ruby 1.9?

Probably not trivially.
Is there a step-by-step written anywhere about how to write an extension
in the Windows environment?

The kicker if you want to use instant rails is you'll need to compile
your stuff using the same compiler that compiled the ruby.exe of instant
rails, which...is old and hard to find.

I'd recommend download the newer versions of ruby [mingw] + devkit, and
google for "ruby c extension" -- once it's all installed you should be
able to build it using
ruby extconf.rb
make

on the command line.
-r
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top