Tenderlove Making

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 <a href="http://github.com/tenderlove/nokogiri/blob/master/ext/nokogiri/extconf.rb#L7">nokogiri extconf</a>.  You can even see where <a href="http://github.com/tenderlove/nokogiri/commit/6321c97963f657e8b4ece1783a6a25ca21504fda">clang helped me shake out some bugs</a>, and I think that's pretty cool.
« go back