Question

I'm writing a C extension for Ruby, and I have something like this in the extconf.rb file:

if(not have_library('z'))
        $stderr << "Error, could not locate zlib.\n"
        abort "Missing zlib"
end

So the installation process aborts if zlib is missing. Instead, since my extension can work with AND without zlib, I would like to do something like the following;

if(have_library('z'))
        # do something so that -DHAVE_ZLIB is passed to the compiler
        # when compiling the extension.
end

How can I do that?

EDIT: The question is on the comment in the if...end statement: how to add the -DHAVE_ZLIB definition for the compiler?

Was it helpful?

Solution

If you can rely on checking for a header file then have_header will define a HAVE_HEADER_H macro for you. For example:

have_header('zlib.h')

will define HAVE_ZLIB_H, which you can then refer to in your code.

If this isn’t enough or you need to define a macro for some other reason you can use the $defs global:

if(have_library('z'))
  $defs << '-DHAVE_ZLIB'
  # Now HAVE_ZLIB will be defined for you to check in your code
end

OTHER TIPS

I would do it as the following your in extconf.rb file

$libs = ["-lliba", "-llibb"]
if have_library('z')
  # where $libs is your array of libraries defined
  $libs << "-llibz"
end

As you can see in the api docs for have_library, the method have_library returns either true or false

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top