Thoughts and tutorials on programming

Thursday, July 23, 2009

How to compile using visual studio for ruby

Extension building in ruby on windows is a little complicated, because the "mainstream" one click installer uses mingw for its build, which is based on a rather outdated msvcrt.dll [version 6 I believe--the one used by Visual Studio 6, no longer in production].

It is, however, according to Charlie Savage, possible to mix code built from newer compilers into it [1].

Mixing runtime libraries

The most obvious criticism of this plan is that it will lead to mixing of microsoft runtime c libraries. From my experience this works as long as extension developers follow the rules described here:

To be more concrete, this boils down to two simple rules:

* If you call ALLOC or ALLOC_N, use xfree and not free
* Don't call sprintf or printf in an extension, instead use rb_f_sprintf/rb_vsprintf/rb_io_printf

If an extension violate these two rules then its obvious, a segmentation fault happens. Thus these bugs are easy to find and easy to fix.

Since VC6 is thankfully no longer available, supporting msvc absolutely, positively requires mixing c runtime libraries and therefore extension writers must follow these two simple rules.

(Note that if you use rb_x_malloc and its siblings, they will use the right free call for you).

i.e. (summarizing) "If it uses ALLOC, xfree, doesn't use STDIN/STDOUT, it can link against any runtime it wants" [though nobu did note there may be a problem with errno's differing, probably not a problem.]

That being said it would appear that you need to also avoid the /MD compiler directive in MS compilers, as well [2].

Also, users may need to download a redistributable for the version of C compiler you used.

However, it should work.
Note that for distributing gems, you can use Luis Lavena's "rake compiler" to build your binaries for you [3].
Good luck!
[3] ex: ext.cross_platform = ['i386-mswin32', 'i386-mingw32']


Roger Pack said...


Using /MD should be fine.

Actually, it sounded more like to me that they didn't compile the dll correctly. The dll has to have a manifest embedded in it - which the the regular version of Visual Studio does but nmake does not. Or they didn't have the redistibutables installed (don't do in manually, run the Microsoft installation program).

Roger Pack said...

might need to install the redistributable for [whatever compiler you want to use, like vc2009].

Melissa said...
might be useful, as well

Roger said...
This comment has been removed by a blog administrator.
Roger Pack said...

might be helpful, too

Roger Pack said...

apparently "long doubles" are incompatible

but ruby "doesn't seem to use those"

rogerdpack2 said...

more fodder:

There might be complications if you use time_t, as well.
"> Also, somewhat unrelated, it appears that long doubles differ in size
> between mingw/msvc 6, and the size of time_t varies from MSVC compiler
> to compiler. Does this mean that using dll's created by those
> compilers might not work with mingw created binaries?

If the APIs of the DLLs includes time_t or long double parameters or
fields in structs pointed to by parameters, etc, then yes indeed."

But I really don't know all the guts.
Presumably if you only used these internally, that would be ok. Something like that.