Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile C++11 code on mac?

Tags:

c++

c++11

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.

like image 946
JASON Avatar asked Jun 22 '13 04:06

JASON


People also ask

Can you compile C on Mac?

C code can be written in any platform like Mac, Windows, etc. C compilers compile C code and create an executable according to the platform. The executable created for one platform can only be executed on that platform. Following are different Mac C compilers.

How do I enable C++ 11 on my Mac?

Go to property of the project --> c/c++ build --> settings --> cross gcc compiler and cross g++ compiler. Put -std=c++11 under miscellaneous part of both tabs. Click Apply.

How do I find C compiler on Mac?

The name of the C compiler (that was installed along with the command line tools) is gcc. To check that this is now successfully installed, enter "gcc --version" at the prompt.


1 Answers

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.
like image 150
Konrad Rudolph Avatar answered Sep 28 '22 19:09

Konrad Rudolph