JASON
JASON

Reputation: 7491

Compile C++11 code on mac?

I'm new to C++11. I've tried:

clang++ -std=c++11 -stdlib=libc++ *.cc

It works, but my questions is:

  1. Is there anyway to set these flag as default for clang++?
  2. How to update g++ 4.2 to a version that supports C++11?
  3. Which way do you think it's the best to compile C++11 code on mac?

Thanks.

Upvotes: 5

Views: 13079

Answers (4)

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

Brett has described how to install GCC it with MacPorts. Here’s how to do it with Homebrew, which styles itself (rightfully!) as a modern replacement of MacPorts:

brew tap homebrew/versions
brew install --enable-cxx gcc48

As far as I know the easiest way to install the most recent Clang is by downloading the compiled version linked in dsign’s answer – and, as Brett mentioned, since Apple uses clang internally it’s not advised to tinker with that installation – just put yours somewhere else.

Concerning what the “best” compiler is there are two things to consider in addition to what Brett has already said:

  1. GCC is much older and more mature than Clang. Internal compiler errors do happen occasionally in Clang. That said, it’s maturing rapidly because it’s being pushed by several companies.
  2. Clang is feature complete for C++11, GCC 4.8 is not. One very obvious example of this is the fact that GCC 4.8 still has no working <regex> implementation, which is a shame.

Upvotes: 7

johnbakers
johnbakers

Reputation: 24770

Do it all in Xcode's build settings like most Apple developers. Xcode simplifies life in many ways.

Upvotes: 1

Brett Hale
Brett Hale

Reputation: 22318

Outside of an IDE (e.g., in shell), I normally have the variable CXX set to: "clang -std=c++11 -stdlib=libc++" in .profile / .tcshrc / etc., since this is picked up by most configure scripts too. On the cmd line I might use: $CXX -c foo.cc

MacPorts gcc-4.8.1 works well: "[sudo] port install gcc48 [-universal]"

"[sudo] port select --set gcc gcc48" will make this the default gcc, g++, etc.

Don't attempt to update or modify the system tools, like the old gcc-4.2 / llvm hybrid that comes with Xcode.

I don't know what you mean by 'best' way in the 3rd part of your question, but with Apple's support (they employ the primary author of LLVM), and other projects like FreeBSD behind it, clang will only continue to improve. It's already much faster than gcc, has far better error messages / diagnostics (especially for C++ and templates), and a modular architecture. For OS X, it's the clear choice.

Upvotes: 5

dsign
dsign

Reputation: 12700

Answers:

  1. I don't think so
  2. You install another g++ version alongside 4.2, it is bad karma to remove the one that comes with the system. To install a new one, check this
  3. Same goes for clang. You can download it here.

In general, I totally recommend that you get better g++ compilers, 4.2 is quite old and its code quality is not as good. And using c++ 11 is totally worth it.

Upvotes: 1

Related Questions