The other day I was attempting to install the FFI Ruby gem when I got this rather interesting error message:

Building native extensions. This could take a while…
ERROR: Error installing ffi:
ERROR: Failed to build gem native extension.
/Users/fc/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb

checking for ffi.h… *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Looking at the error, extconf.rb is failing but I didn’t have access to the mkmf.log file to see what the issue might be. Thankfully FFI is open source (Yay open source!) so I grabbed the code from github and tried to execute the extconf.rb file directly. (The extconf.rb is used by Ruby to compile a Ruby C extension.) I got this same error message as before but now I could take a look at the mkmf.log file, which had the following:

package configuration for libffi is not found
“/usr/bin/gcc-4.2 -o conftest -I/Users/fc/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/x86_64-darwin11.0.0 -I/Users/fc/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/ruby/backward -I/Users/fc/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wshorten-64-to-32 -Wno-long-long -fno-common -pipe conftest.c -L. -L/Users/fc/.rvm/rubies/ruby-1.9.2-p290/lib -L. -lruby.1.9.1-static -lpthread -ldl -lobjc ”
checked program was:
/* begin */
1: #include “ruby.h”
3: int main() {return 0;}
/* end */

Weird. When I tried to run the gcc-4.2 statement in my shell I immediately got an error saying there is no gcc-4.2. A-ha, A clue! In fact when I checked using the which command it turns out there is no gcc-4.2 executable to be found. That seemed odd considering I have xcode installed on my machine. Looking at my /usr/bin directly and grepping for gcc this is what I got:


Ah, that’s right — I have xcode 4.2 installed which now makes use of LLVM. It’s prior versions of xcode that have the common gcc. This is why Ruby was failing when it tried to compile the C extension. With a quick creation of a symbolic link to the llvm-gcc-4.2 called gcc-4.2 that I placed in my /usr/bin everything worked as expected and I had the FFI gem properly installed on my system — Hooray!

In any case, I figured I’d capture this bit of knowledge so that if anyone else happens to come across this they won’t have to struggle so much finding help :).

Note that if you had a prior version of xcode installed on your Mac and then installed xcode 4.2, you’ll likely have both gcc-4.2 and llvm-gcc-4.2 installed. I had xcode 4.1 installed but I uninstalled it and installed xcode 4.2, which is why I didn’t have gcc-4.2. Either xcode will have to be updated to include the symbolic link or Ruby will have to be updated to compensate looking for llvm-gcc-4.2, specifically Ruby’s mkmf gem which is actually responsible for constructing the Makefile to construct a Ruby C extension.