This help site has been deprecated. Please send your requests to

Best way to get optional native extensions in a gem?

Sam's Avatar


11 Mar, 2010 04:54 PM

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.

require 'mkmf'
extension_name = 'sasl'

if find_library("c", "main") and find_library("sasl2")
have_library("c", "main") have_library("sasl2") create_makefile(extension_name) else
puts "Package cyrus-sasl-devel not found, SASL support disabled" end

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?

  1. Support Staff 1 Posted by Eric Hodel on 11 Mar, 2010 09:12 PM

    Eric Hodel's Avatar

    You need to generate a Makefile that does nothing in the else case, and be sure to have a wrapper around the require of that rescues the LoadError.

  2. John Barnette closed this discussion on 17 Nov, 2010 07:22 AM.

Discussions are closed to public comments.
If you need help with please start a new discussion.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac