26

How may I compile a C++ program under the C++11 standard in Ubuntu 11.04 with the most up-to-date compiler, at best using compiler from the distribution, i.e., pre-built package.

loved.by.Jesus
  • 2,266
  • 28
  • 34
tsaarni
  • 818
  • 1
  • 8
  • 18
  • 3
    Technically this is not a programming question. "How do I install X" belongs on SuperUser or somewhere like that. – Warren P Dec 06 '11 at 16:59
  • You can see that the [Ubuntu 12.04 alpha](http://cdimage.ubuntu.com/releases/12.04/alpha-1/precise-server-i386.list) contains GCC 4.6.1, which Hauleth below says has the features you want. You may be able to install that package on your own system with a bit of hacking (I don't know Ubuntu) or you could just install the 12.04 alpha. – Rup Dec 06 '11 at 17:11
  • @WarrenP: Yes, but if you have to compile the compiler yourself, it might belong here - but since he asks for pre-built packages, i agree with you. – smerlin Dec 06 '11 at 17:22
  • Also, he doesn't state what version of ubuntu he's using. My 11.10 has gcc 4.6.1 already, and it supports most of C++11. I am not 100% sure that any version of GCC that has pre-built binaries (released, stable) is "100% C++11" yet, either. So I'm guessing he mostly wants a stable LLVM+GCC C++ binary packages?. – Warren P Dec 06 '11 at 17:27
  • I'm using Ubuntu 11.04. I started by checking out LLVM source and compiling it but then realized I'd probably need newer libstdc++ too. Not sure though if gcc 4.6 would then be better approach. – tsaarni Dec 06 '11 at 17:36
  • 7
    @WarrenP The FAQ says specifically that "software tools commonly used by programmers" are not off topic. It makes sense, programmers would have the most experience installing compilers, not sysadmins. – Tom Kerr Dec 06 '11 at 17:38
  • 1
    So `apt-get` and adding new apt sources is a programmer thing now? – Warren P Dec 06 '11 at 17:54
  • 2
    @Warren P: Possibly. If somebody were to ask if there's a library that does X, it would be reasonable to answer something like "FooLib does; on Ubuntu get it with `sudo apt-get install foo-lib`." In this case, the OP is asking what the most up-to-date compiler available at least indirectly, rather than asking "How do I install gcc 4.6?" The answers are suggesting gcc 4.6, which is on topic. – David Thornley Dec 06 '11 at 18:08
  • If the title was "what C++ compiler on Linux is most C++ 2011 compliant" it would be 100% on topic. I voted to close because of the word "installing" in the title. – Warren P Dec 06 '11 at 19:48

2 Answers2

13

GCC 4.6 provides most of the C++11 features. One has only to add the flag -std=c++0x and can use range based for loops, strong enums, UTF strings, etc. For the list of features available in a given version of GCC one can check on http://gcc.gnu.org/projects/cxx0x.html.

Francesco
  • 3,200
  • 1
  • 34
  • 46
Hauleth
  • 22,873
  • 4
  • 61
  • 112
  • He wants to know where he can get a binary distribution of it, though. – Rup Dec 06 '11 at 17:08
  • 2
    GCC 4.6 is default version of GCC in Ubuntu 11.04 and previous are based on GCC 4.5 with also has some of features (like strongly typed enums). – Hauleth Dec 06 '11 at 17:10
  • 1
    just specify c++11 flag (-std=c++11) when compiling. man says that "Support for C++11 is still experimental, and may change in incompatible ways in future releases." but you can try. for instance: root@ubuntu:~/NetBeansProjects/pointers_string# g++ -std=c++11 -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d - o build/Debug/GNU-Linux-x86/main.o main.cpp root@ubuntu:~/NetBeansProjects/pointers_string# g++ -o dist/Debug/GNU-Linux-x86/pointers_string build/Debug/GNU-Linu x-x86/main.o – 4pie0 Feb 17 '13 at 01:10
  • Is there an environment variable equivalent to setting the flag? – Lori Dec 12 '14 at 18:16
  • I don't think so, but you can write Makefile which will add that flag. For more check http://stackoverflow.com/questions/21221411/when-will-gnu-c-support-c11-without-explicitly-asking-for-it – Hauleth Dec 12 '14 at 20:51
9

Update in 2017: It seem GCC 7 and higher has full C++ 2011 compliance. The rest of the answer below was written in the GCC 4.7 era, and was correct when written in 2011.

Strictly speaking, I can not offer you any 100% C++ 2011 compliant open source compiler, because there aren't any that are 100% there yet.

It looks like you should stay with GCC for now, and GCC 4.6 binaries are included in recent Ubuntu distributions. C++ 2011 is incomplete in GCC 4.6 but contains a lot of 2011 stuff, more than Clang+llvm. It's more than 90% C++ 2011 feature complete. GCC 4.7 contains yet more stuff, but I don't see gcc-4.7 binary packages in Ubuntu 11.x yet, but you can check over here for another way to get gcc 4.7 binaries on ubuntu, or try AskUbuntu, a stackexchange powered site for ubuntu.

According to this page Clang+llvm does not yet offer full C++ 2011 standards compliance, either, and I haven't done the exact math but I see a lot more "No" entries on Clang, versus Gcc.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • Thanks for your answer. GCC 4.7 seems to be the best choice by counting the supported proposals, GCC 4.6 and LLVM 3.0 being about equal. Compiling GCC 4.7 alongside the distro's toolchain seems to be the simplest alternative to get started, but as you mention that discussion should not continue here. – tsaarni Dec 06 '11 at 18:31
  • Building a toolchain from sources seems programming related enough to me. Installing a set of .deb or .rpm binaries seems NOT programming related to me. – Warren P Dec 08 '11 at 22:28