Compiling with Clang 3

Posted by Aaron Patterson on January 03, 2010

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:

require 'mkmf'

RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']

# ... rest of your extconf goes here

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

$ CC=/Developer/usr/bin/clang rake compile

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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Nobuyoshi Nakada Wed, 06 Jan 2010 04:35:44 UTC

    rbconfig.rb may not be compatible?

  2. Aaron Patterson Wed, 06 Jan 2010 10:45:31 UTC

    @Nobu I suppose it’s possible. I believe the ABI output from Clang is compatible with GCC so the extension still works.

  3. Michel Demazure Sun, 17 Jan 2010 01:00:46 UTC

    Nothing to do with Clang, but I don’t know your email.

    It’s about racc : it apparently does not parse the new 1.9 hash syntax. As a consequence, flog and flay abort.

    Am I right ? If so, do you plan something to fix it ? Or to have flog (and flay) use another parser ?

    Flog is a nice tool. It would be a pity to let it go …

    All th best,
    M.

Comments

Check Spelling
Activate Spell Check while Typing