Extending native C if library is available

I am creating my own Ruby gem C extension to generate unique identifiers (found here ). I would like the library to use libuuid , if possible (through the C extensions), and return to a simple Ruby implementation. I currently have C and Ruby code to generate UUIDs, however I cannot figure out how to set up a successful fallback. Any ideas?

+4
source share
1 answer

The have_library method has a return value:

Returns whether the given func entry point was found inside lib .

So you have to do this:

 $defs.push('-DUSE_RUBY_UUID') if !have_library('uuid') create_makefile("identifier") 

And then configure your C to use libuuid if USE_RUBY_UUID not defined and is called into the Ruby UUID library if it is defined.

Oddly enough, have_header and have_func in mkmf.rb add macros for you:

 # File mkmf.rb, line 840 def have_header(header, preheaders = nil, &b) checking_for header do if try_header(cpp_include(preheaders)+cpp_include(header), &b) $defs.push(format("-DHAVE_%s", header.tr_cpp)) true else false end end end 

but have_library makes you do it yourself.

+3
source

All Articles