Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update to C++11?

I am new to programming, so have never experienced a language update. With the release of C++11, I want to make use of all the new features such as lambda expressions and threads. I know I can do this with external libraries but using native language features would be more convenient.

I am using gcc 4.2.1 on Mac OS X Snowleopard in Xcode 3.2.6

What all do I need to do and update to start using C++11 features?

like image 727
fdh Avatar asked Feb 14 '12 04:02

fdh


People also ask

How do I upgrade from C++ 98 to C++11?

Take baby-steps, by first trying to build in C++03 mode, and fix potential errors and warnings. Then rebuild in C++11 mode, and again fix errors and warnings. Each of these steps are not that big, and C++11 is mostly backward compatible to C++03 which is mostly backward compatible with C++98.

Do I have C++11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

What is the use of C++11?

C++11 includes new smart pointer classes: shared_ptr and the recently-added unique_ptr. Both are compatible with other Standard Library components, so you can safely store these smart pointers in standard containers and manipulate them with standard algorithms.

How do I enable C++11 in Dev CPP?

To do this, click on Tools in Dev-C++ IDE. Under this click the “Settings” tab. Inside the settings tab, we can see the “Code generation” tab. Click on the “Language Standard (-std)” value and set it to “ISOC++11” or “GNUC++11” as per your requirement.


2 Answers

You can update to Xcode 4.1 (or whatever the most recent version you can get for Snow Leopard is) and get a new compiler with a few more C++11 features. There are some posts here on Stack Overflow about getting better support for C++11 in Xcode 4.1 on Snow Leopard.

But even the latest compiler available through Xcode does not support some C++11 features like lambdas. To get the best C++11 support you'll want to install a newer compiler, gcc 4.6 or 4.7, or Clang.

I frequently build the latest version of clang from source. It's not difficult to do if you're familiar with building other open source software. I use the git repos for clang and llvm, http://llvm.org/git/llvm.git and http://llvm.org/git/clang.git. You can also find instructions on their website for getting started: http://clang.llvm.org/get_started.html. Once you have the source for clang and llvm it's just ./configure && make && sudo make install. (you might want to run the tests before installing, since this is directly out of the repository. After make do make check in the llvm directory, and once that passes cd down to tools/clang and run make test. If everything is okay then sudo make install)

I don't remember if Snow Leopard included libc++ or not, so you may need to get that as well. http://libcxx.llvm.org/

Once everything is built and installed you can do:

clang++ -std=c++11 -stdlib=libc++ main.cpp && ./a.out

and you should have just about the best C++11 support around.

Recent patches in clang have really improved support for the last features you're likely to notice as a new C++ programmer. There are still a few bits and pieces left, but as of 3.1, and as far as I'm aware, clang has every C++11 feature that either gcc 4.7 or VC++11 has and more besides. libc++ also has the fewest gaps in terms of C++11 standard library features IME (though I think VC++'s standard library will also be pretty complete once they catch up on language features, e.g. char32_t and char16_t as native types so that the standard's mandated specializations for those types can be used).

like image 192
bames53 Avatar answered Oct 11 '22 07:10

bames53


Basically you only need to get a toolchain that has support for the new features. In macosx that would be either GCC or clang++. You might need to download/compile a particular version if needed (i.e. if the feature that you want to try is not in a prepacked compiler bundle but is available in the repository).

I downloaded and compiled the latest trunk of clang++ from subversion to do some testing, and installed g++ 4.6 with macports in Snow Leopard. I remember that I had some issues with the setup of the environment, but don't quite remember which of the compilers gave me problems or how I solved them. Google is your friend there :)

like image 41
David Rodríguez - dribeas Avatar answered Oct 11 '22 07:10

David Rodríguez - dribeas