Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using homebrew, gcc and llvm with C++ 11

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.

like image 566
Abe Schneider Avatar asked Jan 02 '13 19:01

Abe Schneider


3 Answers

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.

like image 82
Matt Yang Avatar answered Nov 07 '22 17:11

Matt Yang


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

like image 39
pepper_chico Avatar answered Nov 07 '22 17:11

pepper_chico


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
like image 1
jassinm Avatar answered Nov 07 '22 16:11

jassinm