2010-01-03 @ 17:05

Compiling with Clang

HI EVERYONE AND HAPPY SUNDAY!

Lately I’ve been trying to compile my ruby extensions with Clang. One reason I like trying out my extensions with Clang is because it catches some errors that GCC doesn’t. If you know the right things to set, it’s pretty easy to get your extension to compile with Clang. Unfortunately finding the right thing isn’t always easy, but I found the right bits to flip and I want to share!

Here’s how to do it. Add this line to your extconf.rb right after you require mkmf:

[ruby] require ‘mkmf’

RbConfig::MAKEFILE_CONFIG[‘CC’] = ENV[‘CC’] if ENV[‘CC’]

… rest of your extconf goes here

[/ruby]

Then when you compile your extension, just set CC to point at clang:

~~~ $ CC=/Developer/usr/bin/clang rake compile</code></pre>

You can see it in action in the nokogiri extconf. You can even see where clang helped me shake out some bugs, and I think that’s pretty cool.

read more »