Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:
- I'm using a mac
- I'm dependent on a bunch of libraries provided by homebrew (and really don't want to compile them myself). Specifically OSG, which itself is dependent on a ton of other libraries. And boost, though I can always compile that myself.
Homebrew seems to only want to use gcc (please correct me if I'm wrong). I can't find any options to switch to LLVM instead. While I understand that this might be due to the fact that not all libraries are compatible with LLVM yet, this would still be a nice feature for those that are.
The version of gcc that comes pre-installed on a mac of gcc is 4.2. gcc 4.2 doesn't have the c++11 features required. I've installed 4.7 via homebrew, but searches for how to set homebrew to use it all say don't do it (gcc 4.2 on the mac is not the vanilla version, so the 4.7 version I got won't be able to compile some things).
My questions are: Does anyone have any suggestions or fixes they have implemented to get around this problem? Should I give up on Homebrew? Does anyone know if Homebrew has a plan to switch to LLVM in the future? Does anyone have any upgrade-plan for how to deal with these incompatibilities?
I don't see how homebrew can continue to depend on gcc 4.2 in the long run, but haven't found any real discussion on this matter.
use
you can also install latest gcc from homebrew-dups
For LLVM,
brew install --HEAD llvm
. To use clang or a brew-installed gcc, add--with-clang
or--with-gcc=gcc-x.x
where x.x is the version you want.The default GCC on Mac is not real GCC of GNU. It's LLVM-GCC in fact, which is a branch of GCC. Several years ago, LLVM-GCC was terminated, and replaced with DragonEgg, which is a GCC plugin to use LLVM as a GCC backend.
LLVM-GCC is just a compiler frontend, whose role is using GCC frontend to translate the source code into LLVM IR[Intro to LLVM 11.3]. Once IR generated, LLVM backend will use it to generate binary code. This step has nothing to do with GCC.
The above goal was fully achieved from 10.7, whose components were all compiled by clang, a frontend provided by LLVM.
But Apple still kept LLVM-GCC and GCC runtime libraries. I guess its purpose might be providing a opportunity to compile some code GCC ONLY.
Now let's answer your questions:
clang++ -stc=c++11 -stdlib=libc++
instead. And clang might have already supported all c++11 features.--with-c++11
argument to use clang to compile boost.But you cannot use this argument whenbrew install autoconf
. In fact, some components might not be compiled correctly by clang.$HOMEBREW_ROOT/Library/Formula
directory. Fortunately, in most of cases, replacing./configure blablabla
with./configure blablabla CXX=clang++ -stc=c++11 -stdlib=libc++
works well. And by the way, if your hack is successful, please make a pull request to homebrew.So, try it and have a fun.
I have an OS X Mountain Lion environment and use C++11. In this answer I'll break your requirement for not compiling your own stuff.
I use Homebrew and, I must say, I advise you to give up on depending on it to provide you clang and libc++ and all its formulas built with them.
What I've done, and I like, is
/opt/local
and put/opt/local/bin
at top on/etc/paths
.I've followed clang build instructions for installing it to
/opt/local
.For libc++, one detail: after running the
buildit
script, I've symlinked the include directory to/opt/local/lib/c++/v1
(clang on/opt/local
looks for this as default directory), and also symlinked the libs to/opt/local/lib/
(but look that binaries will not automatically link to libc++ on/opt/local/lib
. You must use install_name_tool for that).