42

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?

4 Answers 4

39

Now it's enough to run:

brew install llvm

The bottle now includes clang by default.

3
  • 1
    We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.
    – Tetsujin
    Jun 21, 2019 at 11:41
  • 21
    @Tetsujin I agree with you, but this is essentially a full answer. It fully describes what is needed and why Aug 2, 2019 at 7:00
  • 3
    Just for the confused, as it's keg only, the binary for clang will be in e.g. /usr/local/Cellar/llvm/12.0.1/bin/clang or whichever version you have.
    – Rich L
    Oct 5, 2021 at 11:42
24

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"
8
  • Note that if you're doing this for Emscripten, you have to use the clang version they provide.
    – Claudiu
    Jun 21, 2016 at 2:20
  • 8
    The latest Homebrew llvm (3.9.1) in the homebrew-core tap now includes clang, clang++, etc. under /usr/local/opt/llvm/bin Jan 12, 2017 at 17:57
  • 1
    Complement to @ssdecontrol's comment: there's a --with-toolchain argument now that some users might need. More info here, embeddedartistry.com/blog/2017/2/20/installing-clangllvm-on-osx
    – floer32
    Oct 17, 2017 at 19:18
  • 1
    Error: invalid option: --with-toolchain
    – kyb
    Jun 25, 2020 at 11:00
  • 2
    Can someone who knows the current best way to do this clean up all the old/incorrect info from this answer?
    – iono
    Jan 10, 2021 at 9:07
5

The --with-clang and --with-toolchain options don't work anymore. This worked for me:

brew install llvm
cd /Library/Developer/CommandLineTools/Packages/
open .
run the installer

I compiled with

CC=/usr/local/cellar/llvm/7.0.1/bin/clang CCX=/usr/local/cellar/llvm/7.0.1/bin/clang++ make
1
  • 1
    Unfortunately homebrew binary clang is broken. It doesn't include /usr/local/include folder as base include dir, so all software build fails. Clang provided by xcode doesn't have fsanitize=leak and ccc-analyzer. So both clangs on mac os is broken. You have to build it with all features enabled from source. But compilation will kill your CI like travis. So you have to host and maintain separate CI only for apple. Apple is good only for simple development, anything else requires pain.
    – puchu
    May 10, 2019 at 17:19
4

You have to install it with --with-clang option:

$ brew install --with-clang llvm
==> Installing dependencies for llvm: cmake
==> Installing llvm dependency: cmake
==> Downloading https://homebrew.bintray.com/bottles/cmake-3.4.3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring cmake-3.4.3.el_capitan.bottle.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/cmake
==> Summary
🍺  /usr/local/Cellar/cmake/3.4.3: 1,980 files, 27.4M
==> Installing llvm
==> Downloading http://llvm.org/releases/3.6.2/llvm-3.6.2.src.tar.xz
######################################################################## 100.0%
==> Downloading http://llvm.org/releases/3.6.2/cfe-3.6.2.src.tar.xz
######################################################################## 100.0%
==> cmake -G Unix Makefiles /private/tmp/llvm20160211-42310-16fdrbw/llvm-3.6.2.src -DCMAKE_C_FLAGS_RELEASE=-DNDEBUG -DCMAKE_CXX_FLAGS_RELEAS
==> make
==> make install
==> Caveats
LLVM executables are installed in /usr/local/opt/llvm/bin.
Extra tools are installed in /usr/local/opt/llvm/share/llvm.

This formula is keg-only, which means it was not symlinked into /usr/local.

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

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/llvm/lib
    CPPFLAGS: -I/usr/local/opt/llvm/include


If you need Python to find bindings for this keg-only formula, run:
  echo /usr/local/opt/llvm/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/llvm.pth
==> Summary
🍺  /usr/local/Cellar/llvm/3.6.2: 1,350 files, 338.2M, built in 21 minutes 18 seconds

Then, you will find clang++ on /usr/local/opt/llvm/bin/clang++.

3
  • 1
    Hm, is it possible to still get a pre-built binary package (i.e. a 'bottle')? I mean when specifying --with-clang? I am asking because I intend to call that in a continuous integration environment ... Feb 11, 2016 at 14:45
  • If exists, I down know about it, but I don't think that exists.
    – jherran
    Feb 12, 2016 at 11:25
  • 1
    Actually, there is a bottled version available via Homebrew Versions. See also my answer. Feb 13, 2016 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .