Installing LLVM/Clang on OSX

Updated: 20190627

If you are a developer using clang or gcc on OS X, 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 OS X.

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

If you see an older llvm version, I recommend updating Homebrew. Your taps may need to be updated to pick up the newest versions.

To update Homebrew, simply run:

$ brew update
$ brew upgrade

After that, you should be able to check the llvm information again and see a newer version.

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/

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

Further Reading

Update Log

  • 20190627:
    • Corrected OSX to OS X (Thanks Peter Mortensen)
    • Removed deprecated reference to the embedded-resources Makefile