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 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/
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.
Further Reading
- Homebrew: OS X package manager
- Getting Started: Building and Running Clang
Update Log
- 20190627:
- Corrected OSX to OS X (Thanks Peter Mortensen)
- Removed deprecated reference to the embedded-resources Makefile
Hi! Thank you for putting this guide together! One question: how do you change compiler settings to use llvm-ar and llvm-ld?
Hey Andrei, glad you liked the guide.
That question is dependent upon your build system, but generally there are similarly defined LD and AR variables (like CC and CXX).
You can then define AR and LD to be:
export AR := /usr/local/opt/llvm/bin/llvm-ar export LD := /usr/local/opt/llvm/bin/llvm-ld
When I change the compiler variables for homebrew clang:
export CC := clang export CXX := $(CC)++
Every time I open a new terminal, it prompt "clang: error: no input files". How can I avoid it?
And when I use Clang-tidy, something wrong:
➜ Downloads clang-tidy a.cpp -checks=-*,misc-use-after-move -- 1 error generated. Error while processing /Users/feizhao/Downloads/a.cpp. /usr/local/Cellar/llvm/4.0.1/bin/../include/c++/v1/wchar.h:119:15: error: 'wchar.h' file not found [clang-diagnostic-error] #include_next <wchar.h> ^
I don’t know how to solve it.
Hi, do you know how to get latest llvm on Mavericks? my attempt with brew gives build errors, 4.x is fine, not the latest though? msg to: taomarku@gmail.com
Hi Marky,
I managed to get 5.0 to build successfully today. What errors did you get when you tried to compile 5.0 on your system?
"OS X", not "OSX". See:
en DOT wikipedia DOT org/wiki/OS_X
Unfortunately there seems to be no homebrew support for LLVM on Big Sur yet (29 December 2020) (at least on the M1)
$ brew install llvm Error: llvm: no bottle available! You can try to install from source with e.g. brew install --build-from-source llvm Please note building from source is unsupported. You will encounter build failures with some formulae. If you experience any issues please create pull requests instead of asking for help on Homebrew's GitHub, Twitter or any other official channels.
The –with-toolchain option seems to no longer be recognized.