# RUBY\_VERSION\_\* macros in C?

**URL:** <https://www.ruby-forum.com/t/ruby-version-macros-in-c/191324>\
**Category:** Ruby\
**Created:** [July 9, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/ruby-version-macros-in-c/191324 "2010-07-09T16:39:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [July 9, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/ruby-version-macros-in-c/191324/1 "2010-07-09T16:39:26Z")

</div>

Hi there,

I’m trying to create a C extension that compiles for both Ruby 1.8 and  
1.9. As far as I know, there should be the macros RUBY\_VERSION\_MAJOR,  
RUBY\_VERSION\_MINOR and RUBY\_VERSION\_TINY to check with which Ruby  
version we’re compiling, and they should be defined in the header  
“ruby/version.h”. But there’s the problem: That file just doesn’t exist  
in my Ruby installations. Wheather I try to compile with 1.8.7 or 1.9.1,  
I get:

fatal error: ruby/version.h: No such file or directory

## This simple C extension demonstrates the issue:

#include “ruby.h”  
#include “ruby/version.h”

VALUE Foo;

static VALUE m\_abc(VALUE self)  
{  
#if RUBY\_VERSION\_MAJOR == 1 && RUBY\_VERSION\_MINOR == 9  
printf(“Using 1.9\n”);  
#else  
printf(“Using 1.8\n”);  
#endif  
return Qnil;  
}

## void Init\_abc() { Foo = rb\_define\_module(“ABC”); rb\_define\_module\_function(Foo, “abc”, m\_abc, 0); }

If I comment out ‘#include “ruby/version.h”’ I don’t get compile errors,  
but when I run the program, I always get “Using 1.8” regardless which  
Ruby the extension was compiled for.

Here’s my systems configuration:  
Windows Vista 32-Bit  
ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]  
ruby18 -v: ruby 1.8.7 (2009-12-24 patchlevel 248) [i386-mingw32]  
gcc --version: gcc.exe (GCC) 4.5.0  
(I have a separate MinGW + MSYS installation, not the Development Kit  
from the RubyInstaller)

I installed 1.9 via the RubyInstaller’s 7z and compiled 1.8 myself.

Any hints on how to find out the Ruby version from C?

Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [July 10, 2010, 12:09am UTC](https://www.ruby-forum.com/t/ruby-version-macros-in-c/191324/2 "2010-07-10T00:09:01Z")

</div>

Luis L. wrote:

> What you can try to do is use the extconf process to create you the  
> needed defines.
> 
> [do/do\_mysql/ext/do\_mysql/extconf.rb at master · datamapper/do · GitHub](http://github.com/datamapper/do/blob/master/do_mysql/ext/do_mysql/extconf.rb#L77-79)
> 
> HTH,

## Thank you, Luis! That helped me a lot, I now solved the problem by adding this to my extconf.rb:

## print("Checking whether we are running Ruby 1.9… “) if RUBY\_VERSION \>= “1.9.0” $CFLAGS \<\< " -DUSING\_RUBY\_19” puts “yes” else puts “no” end

And in the C code I check wheather USING\_RUBY\_19 is defined.

Thank you!  
Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [July 9, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/ruby-version-macros-in-c/191324/3 "2010-07-09T19:16:42Z")

</div>

On Jul 9, 11:39 am, Marvin Gülker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> fatal error: ruby/version.h: No such file or directory  
> #if RUBY\_VERSION\_MAJOR == 1 && RUBY\_VERSION\_MINOR == 9  
> Foo = rb\_define\_module(“ABC”);  
> ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]  
> ruby18 -v: ruby 1.8.7 (2009-12-24 patchlevel 248) [i386-mingw32]  
> gcc --version: gcc.exe (GCC) 4.5.0  
> (I have a separate MinGW + MSYS installation, not the Development Kit  
> from the RubyInstaller)
> 
> I installed 1.9 via the RubyInstaller’s 7z and compiled 1.8 myself.
> 
> Any hints on how to find out the Ruby version from C?

Most of the times you work around specific Ruby 1.9 features that  
differ from 1.8, for example, encodings.

You have one example here:

> <https://github.com/datamapper/do/blob/master/do_mysql/ext/do_mysql/do_mysql.c>

As you can see, ruby/version.h has been deprecated.

You can find more about these changes here:

[http://blog.grayproductions.net/articles/getting\_code\_ready\_for\_ruby\_19](http://blog.grayproductions.net/articles/getting_code_ready_for_ruby_19)

What you can try to do is use the extconf process to create you the  
needed defines.

> <https://github.com/datamapper/do/blob/master/do_mysql/ext/do_mysql/extconf.rb#L77-79>

HTH,
