tag:help.rubygems.org,2010-01-19:/discussions/questions/12-best-way-to-get-optional-native-extensions-in-a-gemRubyGems.org: Discussion 2011-04-07T20:59:26Ztag:help.rubygems.org,2010-01-19:Comment/11972742010-03-11T16:54:06Z2010-03-11T16:54:07ZBest way to get optional native extensions in a gem?<div><p>I've been puzzling on how to do this correctly all morning. I am
packaging up a gem that can optionally use a shared library to
enhance the functionality. If that library exists the gem I compile
a small c file that has a ruby binding for that library, I can then
try to require that wrapper when I want to use the extra
functionality. If it doesn't exist a warning would be a bonus I
would just like it to keep installing without failing. How am I
supposed to do this? I have resorted to just commenting out the
extension line and changing platform in the gem spec for now but
that seems like a heavy handed approach when it isn't really a
platform specific issue.</p>
<p>require 'mkmf'<br>
extension_name = 'sasl'</p>
<p>if find_library("c", "main") and find_library("sasl2")<br>
have_library("c", "main") have_library("sasl2")
create_makefile(extension_name) else<br>
puts "Package cyrus-sasl-devel not found, SASL support disabled"
end</p>
<p>It correctly notices that the library isn't there but then
considers it to have failed anyways and stops the install there.
Whats the best way to make this happen?</p></div>Samtag:help.rubygems.org,2010-01-19:Comment/11972742010-03-11T21:12:23Z2010-03-11T21:12:23ZBest way to get optional native extensions in a gem?<div><p>You need to generate a Makefile that does nothing in the else
case, and be sure to have a wrapper around the require of sasl.so
that rescues the LoadError.</p></div>Eric Hodel