Using homebrew, gcc and llvm with C++ 11

18,428

Solution 1

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:

  • If you want to use C++11 features, use clang++ -stc=c++11 -stdlib=libc++ instead. And clang might have already supported all c++11 features.
  • If you want homebrew supporting LLVM, it has already supported, at least on backend.
  • If you want homebrew using clang as a compiler frontend, it depends on homebrew community schedule. For example, you can append --with-c++11 argument to use clang to compile boost.But you cannot use this argument when brew install autoconf. In fact, some components might not be compiled correctly by clang.
  • If you know it can be compiled by clang but homebrew hasn't supported yet, you have to hack the corresponding ruby script at $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.

Solution 2

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

  • clone llvm, clang and libc++ from repositories.
  • install to /opt/local and put /opt/local/bin at top on /etc/paths.
  • build my development stuff with my new clang.
  • let Homebrew for installing tools like git and things I'll not develop for, just use.

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).

Solution 3

use

clang++ -std=c++11 -stdlib=libc++ 

you can also install latest gcc from homebrew-dups

brew install [flags] https://raw.github.com/Homebrew/homebrew-dupes/master/gcc.rb
Share:
18,428
Abe Schneider
Author by

Abe Schneider

Updated on June 12, 2022

Comments

  • Abe Schneider
    Abe Schneider almost 2 years

    Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:

    1. I'm using a mac
    2. 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.

  • Abe Schneider
    Abe Schneider over 11 years
    Unfortunately if the libraries are compiled using libstdc++ with homebrew and I use libc++ bad things will happen. In theory the two libraries are supposed to be mostly compatible, but std::strings are implemented differently. I did install the newer version of gcc, but I am of the understanding that homebrew will still use 4.2 (due to the changes Apple made to it).
  • Abe Schneider
    Abe Schneider over 11 years
    I'm actually okay with the current version of clang/libc++. My main problem is having libraries that rely on stdlibc++. Bad things generally happen when you try to mix libraries that use stdlibc++ with your own code that uses libc++.
  • oblitum
    oblitum over 11 years
    @AbeSchneider sure. I like a newer version to follow closer for fix and enhancements. But, anyway, it's libc++, and Homebrew formulas may not support it, so I just gave up on Homebrew for providing libraries. I feel it's best to do it myself.
  • Abe Schneider
    Abe Schneider over 11 years
    Yeah, I'm starting to come to that conclusion -- I was just really hoping that someone else had come up with a solution. Homebrew is really nice, and it would be awesome if I could easily switch it to using libc++. If I were just using boost, it wouldn't be a big deal, but some of the libraries I use have a lot of dependencies, making the compiling on my own option potentially a lot of work. I guess I could try forking homebrew, but I've been avoiding learning ruby...
  • Steve Benner
    Steve Benner about 10 years
    Wow, this was incredibly informative. Thank you for the information here, it helped me understand the history of C compilers in mac OSX much better.