Installing LLVM/Clang on OSX

If you are a developer using clang or gcc on OSX, the odds are good that you are using the Xcode build tools.

These tools work well when compiling for your host machine. You can even get the iPhone SDK to work well enough for embedded development using ARM. However, I have encountered many cases where using the Apple clang compiler has resulted in odd behavior or unexpected errors after updating Xcode.

As a result, I recommend using the mainline clang for your embedded development. Using mainline clang on my system proved to take a little more work than I expected, so I hope my notes make setup easier for you.

Manual Compile + Install

If you would like to go the manual compile+install route, please check this guide from the LLVM group. The manual compile+install instructions are also applicable for those not using OSX.

Installing with Homebrew

I use Homebrew to manage extra packages, and I highly recommend it.

Using Homebrew, we can install llvm/clang very easily. Simply run this command:

$ brew install --with-toolchain llvm

It may be worth running the following command first if you already have an existing homebrew installation:

$ brew info llvm

Seeing llvm v3.6 listed? I recommend updating homebrew, as the new taps have llvm v3.9.1. llvm v3.6 is still lacking in many of the more recent C and C++ features that we'll want to use.

To update homebrew, simply run:

$ brew update
$ brew upgrade

After that, you should be able to check the llvm information again and see v3.9.1 (or later)

Where to find llvm/clang

When you install llvm with brew, the new binaries will not automatically be in the path. Note what the formula says:

OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

You can find the binaries here:

$(brew --prefix llvm)/bin

If you want the Homebrew llvm/clang to show up in your PATH, run the following command:

$ echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile

Building and Linking

Here are the compiler variables you should use for homebrew clang:

export CC := clang
export CXX := $(CC)++

You could also specify the path manually if you don't want to edit your PATH variable:

export CC := /usr/local/opt/llvm/bin/clang
export CXX := $(CC)++

Note that if your tools look for ld or ar, you will likely end up using the Apple tools. Make sure you change your compiler settings to llvm-ar and llvm-ld if you want to utilize the mainline tools.

You will need to add the following flags for compiling and linking:

LDFLAGS += -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib
CPPFLAGS += -I/usr/local/opt/llvm/include -I/usr/local/opt/llvm/include/c++/v1/

You can also check whether brew clang is actually installed before adding these flags. This will help you support users who install clang by other means.

ifeq ($(shell brew info llvm 2>&1 | grep -c "Built from source on"), 1)
#we are using a homebrew clang, need new flags
LDFLAGS += -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib
CPPFLAGS += -I/usr/local/opt/llvm/include -I/usr/local/opt/llvm/include/c++/v1/
endif

If you're getting errors about missing headers like assert.h, make sure to run xcode-select --install. This will populate /usr/include correctly.

Want to test your new clang installation? Checkout the embedded-resources github repo! If you look at the compiler.mk file, you will notice that I have implemented a very simple compiler detection scheme. I try to use, in order:

  1. Clang
  2. Apple Clang
  3. GCC

Give your new compiler a test drive by running make in the embedded-resources repo.

Further Reading