12

This all seems like a colossal mess.
All I want is a compiler that implements C++11, so I can use <chrono>. But I'm so confused from the very beginning.

Currently, I build programs by invoking G++, but when I check the version via $ g++ -v, I get:

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)


What's going on? Am I using G++? GCC? LLVM? I don't even know. Are they the same thing?


So now I'm trying to build and download GCC 4.7 via gnu.org, but I have no idea what any of the guides are talking about. I've never seen so many acronyms for things I dont know.

Why is this so complicated? What's with all those versions, with some of them only implementing some parts of C++11 and not others?

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
gone
  • 2,587
  • 6
  • 25
  • 32
  • Looks like you're on a Mac. You can use [MacPorts](http://www.macports.org/) to install a modern version of GCC. Compiling GCC from source is a little complicated so I think you'll want MacPorts. – Seth Carnegie Feb 01 '13 at 23:36
  • @SethCarnegie thanks that! I used to click and install. Now it takes a following a book of command line commands to install anything. Like what. – gone Feb 01 '13 at 23:40
  • Read [this section](http://guide.macports.org/#using) of the manual, and you can do `sudo port install gcc47` after installing MacPorts, and you'll have to find the command for using g++ 4.7 (I can't remember it). Also, I think Macs come with a modern Clang installed. – Seth Carnegie Feb 01 '13 at 23:45
  • @SethCarnegie ya, but compiling with `clang` i get `fatal error: 'chrono' file not found` – gone Feb 01 '13 at 23:50
  • Yeah, you have to tell it where your standard lib is and stuff or something, which I don't know how to do. You can check how XCode calls it via the options or something. But I avoid all that and just install GCC with that command I showed you. – Seth Carnegie Feb 01 '13 at 23:50
  • You could always just use [Boost.Chrono](http://www.boost.org/libs/chrono/) instead... – ildjarn Feb 07 '13 at 01:17

3 Answers3

22

Here's the situation on OS X.

There are two C++ compilers installed by default.

[5:49pm][wlynch@watermelon ~] g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

[5:49pm][wlynch@watermelon ~] clang++ --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)

g++ is running llvm-gcc, which is the gcc frontend, and then the llvm backend.

clang++ is running clang, which is the clang frontend and then the llvm backend.

If you want a C++11 compiler on OS X without installing other packages, your only option is to use the clang compiler.

The flags necessary are:

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

To describe the two flags I'm passing:

  • -stdlib=libc++ uses the libc++ standard library, instead of the gnu libstdc++. On OS X, the libc++ version has c++11 support. The gnu libstdc++ one does not.
  • -std=gnu++11 tells the compiler to support c++11 code features, like lambdas and enum class. You can also pass -std=c++11, which is similar, but does not enable some commonly expected gnu extensions.

Update for OS X 10.9: As of OS X Mavericks, both g++ and clang++ are actually using clang. The only difference, is that g++ will imply -stdlib=libstdc++ and clang++ will imply -stdlib=libc++. So, on Mavericks, if you'd like to use C++11, you can follow the above advice, or just do:

clang++ -std=gnu++11

Update for OS X 10.10: As of OS X Yosemite, g++ is still clang in disguise. However, neither uses libstdc++ by default anymore. Both are now on libc++.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • lol. Thank you!! So confusing, thank you for your explanation as well! So the llvm backend does the actual compiling regardless, with either gcc/clang doing the parsing? so the `-stdlib=libc++` tells the compiler where to find the libraries I want and the '-std=gnu++11` tells the compiler what version to follow? Why is gnu even involved in any of this? – gone Feb 02 '13 at 00:04
  • [This post](http://stackoverflow.com/questions/10613126/what-are-the-differences-between-std-c11-and-std-gnu11) answers the differences between `-std=gnu++11` and `-std=c++11`. The short answer, is that gcc described some extensions to c++ that are often desirable. Clang optionally implements them. – Bill Lynch Feb 02 '13 at 00:26
  • You are also correct with your statement that the LLVM backend does the actually compiling regardless, and then gcc or clang is doing the parsing. – Bill Lynch Feb 02 '13 at 00:26
  • 1
    The final thing to note, is that GCC and libstdc++ do support C++11. However, Apple only includes older versions of the software (that don't support it) because of licensing fears (GPLv3). – Bill Lynch Feb 02 '13 at 00:29
0

It sounds like you have Xcode 4.6 and the latest command line tools. This is from the release notes:

Important: The LLVM GCC compiler does not include the latest Objective-C and 
C++11 features. Xcode 4.6 is the last major Xcode release which includes the 
LLVM GCC compiler and the GDB debugger. Please migrate your projects to use the 
LLVM compiler and LLDB debugger…

I think you want to use c++ instead:

$ c++ -v
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
eager
  • 627
  • 6
  • 10
  • Thank you for the response. what is `c++`?? Man I'm so confused. Are all these compilers the same? Different? Kind of different? – gone Feb 01 '13 at 23:45
  • @ZacharyO'Keefe `c++` is an alias for `clang++`. – Seth Carnegie Feb 01 '13 at 23:45
  • @SethCarnegie is `clang++` different from `clang`? Man. lol. what is `llvm-gcc`? is it llvm? gcc? – gone Feb 01 '13 at 23:48
  • `clang++` is to `clang` as `g++` is to `gcc`; one is for C++, the other is for C. `llvm-gcc` is a version of GCC that targets the LLVM for code gen. – Seth Carnegie Feb 01 '13 at 23:49
0

@sharth: The situation changed on the Mac quite significantly since the release of XCode 5.0. clang/clang++ are the default C/C++ compilers. They correspond to the LLVM version 3.3 I believe, and this version of clang++ is fully C++11-compliant. Note that clang++ --version will return a version number like "5.0.x" on the Mac but that refers to the XCode version.

I have been using the Apple clang++ in a C++11 project for months now and so far I have not seen any problems. There is absolutely no reason to use any other C++ compiler on the Mac just now :-)

The situation with GCC/G++ is not so rosy. The latest version of G++ (4.8.2) does implement most of the C++11 standard, however the standard library is not compliant! For instance, std::regex is not implemented in libstdc++, but you find this out only when you run your code and the regex constructors throw std::regex_error -s. (I found this out the hard way when trying to port the aforementioned little project to Linux.) The community believes full compliance will be achieved with the 4.9 release of G++. Until then you should use the Clang compilers on Linux as well.

I have no access to the latest Intel C++ compiler suite so have no idea how compliant icpc is.

András Aszódi
  • 8,948
  • 5
  • 48
  • 51