RUBY_VERSION_* macros in C?

  • Thread starter Marvin Gülker
  • Start date
M

Marvin Gülker

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
 
L

Luis Lavena

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?

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:

http://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

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

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

HTH,
 
M

Marvin Gülker

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

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top