Step 1. Install the operating system.
Step 2. Make sure the meta-package named "build-essential" has been install correct in the ubuntu system.
- $ sudo apt-get install build-essential
Step 3. Add the necessary PPA on the ubuntu system.
A quick search on Google turned up a PPA with the newer GCC v4.7, which has support for nearly all the new C++11 features. Add this PPA with the following commands:
- $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
- $ sudo apt-get update
Step 4. Install the GCC v4.7 C/C++ compilers.
- $ sudo apt-get install gcc-4.7 g++-4.7
Step 5. Switching compilers.
Installing the v4.7 compilers does not remove the previous version compiler. This is very important, if you skipped the previous sentence, now is a good time to go back and re-read it! The system now has 2 'C' compilers and 2 'C++' compilers:
- $ which g++
- /usr/bin/g++
- $ ls -lh /usr/bin/g++
- lrwxrwxrwx 1 root root 7 Dec 12 17:04 /usr/bin/g++ -> g++-4.6
- $ ls -lh /usr/bin/g++*
- lrwxrwxrwx 1 root root 7 Dec 12 17:04 /usr/bin/g++ -> g++-4.6
- -rwxr-xr-x 1 root root 349K Dec 17 04:17 /usr/bin/g++-4.6
- -rwxr-xr-x 1 root root 566K Dec 22 02:37 /usr/bin/g++-4.7
Luckily, there is a very simple and standard method for switching between applications that provide identical (or near-identical) functionality: update-alternatives. If you've never had to use this before, go ahead and run man update-alternatives.
We need to let update-alternatives know we have 2 C/C++ compilers, create a record for each one, and then configure which one we want to use. This is done with the following:
- $sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
- $sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7
- $sudo update-alternatives --config gcc
Note the use of "slave". This ensures when we change the configuration for gcc, that we automatically update the configuration for g++. This is important so the compilers aren't out-of-sync.
From this point forward, the only thing required when switching compilers is this (relatively) simple command:
- $sudo update-alternatives --config gcc
Calling the C++ compiler with --version confirms the right version is correctly installed:
- $ g++ --version
- g++ (Ubuntu/Linaro 4.7-20111222-0ubuntu1) 4.7.0 20111222 (experimental) [trunk revision 182617]
Step 6. Test the compilers.
I will not say anything about it. The reader can code and test the C++11 features himself.
Reship from
阅读(1696) | 评论(0) | 转发(0) |