Conditional compilation of C extensions in Ruby 1.9Edit
Was just wondering how to do conditional compilation of a Ruby C extension for Ruby 1.8/1.9.
My first attempt didn’t work, but I was able to implement a working solution after finding this tip here.
It basically consists of adding some lines like this to your extconf.rb
:
case RUBY_VERSION
when /\A1\.8/
$CFLAGS += ' -DRUBY_1_8_x'
when /\A1\.9/
$CFLAGS += ' -DRUBY_1_9_x'
else
raise "unsupported Ruby version: #{RUBY_VERSION}"
end
And then in your C code you can:
#if defined(RUBY_1_9_x)
...
[/tags/elif #elif] defined(RUBY_1_8_x)
...
[/tags/else #else]
[/tags/error #error] unsupported RUBY_VERSION
[/tags/endif #endif]