Homebrew Development – Installing Recent Clang++ with Homebrew

developmenthomebrew

I've tried

brew install llvm

but after that I cannot find any clang++* executable under /usr/local.

Thus my question: How to get clang++ via Homebrew?

Best Answer

As of 2018, the Homebrew Versions repository ('tap') is out of service.

The stock Homebrew llvm package still doesn't include clang/clang++, by default. Thus, it isn't part of the prebuilt ('bottled') package.

One can build it via:

brew install --with-toolchain llvm

And then use it via e.g.:

$ PATH="/usr/local/opt/llvm/bin:$PATH" \
    LDFLAGS='-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib' \
    cmake ...

But this --with-toolchain induced build takes a very long time and is thus unsuitable in a continuous integration (CI) environment.

However, the clang that comes with recent XCode (which is available in CI environments like Travis-CI, Version 9 is the default, 10 available, too) isn't that outdated anymore as it used to be (Apple uses an fantasy version scheme for clang that doesn't match upstream clang version numbers but cmake detects e.g. for AppleClang 9.1.0.9020039 the version 4.0.1). Thus, it is sufficient to build C++11/C++14 software with common dependencies like Boost (e.g. version 1.67).

2016 State of the Art

The llvm package in Homebrew doesn't include clang++, by default. When installing it, you have to add --with-clang to the command line (e.g. brew install --with-clang llvm). The extra --with-clang yields a full package compile because there is only one prebuild ('bottled') llvm package available (without clang++). In addition to that: the llvm package is relatively old - currently it has llvm 3.6 - where 3.7 was released 6 months ago.

Thus, to get a bottled clang++ 3.7 you have to install the llvm package from Homebrew Versions:

$ brew tap homebrew/versions
$ brew install llvm37

It is then available under:

/usr/local/bin/clang++-3.7

The formula also notes:

To link to libc++, something like the following is required:
  CXX="clang++-3.7 -stdlib=libc++"
  CXXFLAGS="$CXXFLAGS -nostdinc++ -I/usr/local/opt/llvm37/lib/llvm-3.7/include/c++/v1"
  LDFLAGS="$LDFLAGS -L/usr/local/opt/llvm37/lib/llvm-3.7/lib"