RUBY_VERSION_* macros in C?

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
--
Posted via http://www.ruby-forum.com/.

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:

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

You can find more about these changes here:

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

HTH,

···

On Jul 9, 11:39 am, Marvin Gülker <sutn...@gmx.net> wrote:

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?

--
Luis Lavena

Luis Lavena 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

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
--
Posted via http://www.ruby-forum.com/\.