Installing image_science on OS X with Homebrew,

published at 9:11am on 11/02/11

I just wanted to write about a bug that ate up a large chunk of my day yesterday (and a large chunk of a day in June, as well, but I forgot to write about it last time and therefore forgot how I had solved it before).

This is a bug that’s only going to affect people who:

Everyone else can just walk away at this point. Maybe peruse twitter, which always ends up sucking away half my day anyway.

In any event, I installed FreeImage using homebrew and installed the image_science gem without a problem. But when I tried to require the gem in irb, I would get this error:

/Users/jcn/.ruby_inline/Inline_ImageScience_cdab.c:2:23: error: FreeImage.h: No such file or directory
/Users/jcn/.ruby_inline/Inline_ImageScience_cdab.c: In function ‘unload’:
/Users/jcn/.ruby_inline/Inline_ImageScience_cdab.c:8: error: ‘FIBITMAP’ undeclared (first use in this function)

CompilationError: error executing “cc -dynamic -bundle -undefined suppress -flat_namespace -fno-common -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch x86_64 -fno-common -pipe -fno-common -I /Users/jcn/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/x86_64-darwin10.6.0 -I /Users/jcn/.rvm/rubies/ruby-1.8.7-p330/include -L/Users/jcn/.rvm/rubies/ruby-1.8.7-p330/lib -o \”/Users/jcn/.ruby_inline/Inline_ImageScience_cdab.bundle\” \”/Users/jcn/.ruby_inline/Inline_ImageScience_cdab.c\” -lfreeimage -lfreeimage -lstdc++ “: 256

and so on. So that first line obviously indicates that image_science (which uses RubyInline to compile inline C code at runtime) was not able to find the FreeImage header file, and therefore couldn’t compile the pieces of itself. The last line shows the directories that the compiler is using to look for headers, and we will note that /usr/local/include (which is where homebrew puts its header files) is not listed.

The first thing I thought was that I needed to pass a -I flag in to the gem as it was being installed, but I realized that this would do nothing since the RubyInline compilation is happening at runtime, not at installation time.

So how do you tell your compiler where to find your include files if you’re compiling a C program? Well, a little “man gcc” led me to this:

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

CPATH specifies a list of directories to be searched as if
specified with -I, but after any paths given with -I options on the
command line. This environment variable is used regardless of
which language is being preprocessed.

Perfect! Since image_science just shells out to cc at runtime when it needs its C components built, it should just pick up the CPATH as well. So I dropped the following into my .bashrc, and everything is hunky dory now:

export CPATH=/usr/local/include

So just remember, kids, all those things you learned back in school that you thought you left behind when you discovered Ruby continue to prove useful from time to time. Hooray for man pages and knowing how to read error messages!

(Incidentally, this bug has been fixed in the main repo for the image_science gem, but they never bumped a new version with this fix.)

Filed under: Technology

At 10:38 pm on 01.02.14, Bryan Braun said,

Just as an update, it looks like the Homebrew fix made it into version 1.2.2 and there have been a couple of version updates since then. You can see the release history at https://github.com/seattlerb/image_science/blob/master/History.txt

Leave a Reply: