How to install and use GCC g++ v4.7 and C++11 on Ubuntu 12.04
2013-02-18 20:25
1056 查看
from: http://charette.no-ip.com:81/programming/2011-12-24_GCCv47/
How to install and use GCC g++ v4.7 and C++11 on Ubuntu 12.04 (beta)
2011-12-24_GCCv47.cpp
I had a bit of extra spare time over the 2011 holiday season, and I wanted to test out some of the new C++11 features. But I know the old GCC C++ compiler I have installed in my usual LTS Ubuntu 10.04 development environment is out-of-date, so the first
thing I needed to do is upgrade my OS and GCC to something decent. Here is how I accomplished this task.
First thing was to change my operating system. From Canonical, I downloaded the latest daily live .iso file. Here is a decent place to start looking:http://cdimage.ubuntu.com/daily-live/current/precise-desktop-amd64.iso.
These live-cd images are a breeze to install using VirtualBox, and within 30 minutes I had a decent development environment installed.
When Ubuntu 12.04 is first installed, it defaults to GCC v4.6. And just as important, it only includes the 'C' compiler, not the 'C++' compiler:
$ which g++ gcc/usr/bin/gcc$ gcc --versiongcc (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2
A number of development tools are referenced by the meta-package named build-essential, so I started by making sure this package is installed:
$ sudo apt-get install build-essential
This gave me the expected GCC v4.6 C++ compiler:
$ which g++ gcc/usr/bin/g++/usr/bin/gcc$ g++ --versiong++ (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2
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
Now take a look to see if we can find the GCC packages:
$ sudo apt-cache search "g\+\+"...g++ - GNU C++ compilerg++-multilib - GNU C++ compiler (multilib files)g++-4.4 - GNU C++ compilerg++-4.4-multilib - GNU C++ compiler (multilib files)g++-4.5 - The GNU C++ compilerg++-4.5-multilib - The GNU C++ compiler (multilib
files)g++-4.6 - GNU C++ compilerg++-4.6-multilib - GNU C++ compiler (multilib files)g++-4.7 - GNU C++ compilerg++-4.7-multilib - GNU C++ compiler (multilib files)...
More information on the multilib packages can be found here, though most people shouldn't need it.
Install the GCC v4.7 C/C++ compilers with the following command:
$ sudo apt-get install gcc-4.7 g++-4.7
Installing the v4.7 compilers does not remove the previous v4.6 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.6sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7sudo 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++ --versiong++ (Ubuntu/Linaro 4.7-20111222-0ubuntu1) 4.7.0 20111222 (experimental) [trunk revision 182617]
The only thing left is to test if the new v4.7 compiler is working as expected. There are several convenient pages on the GNU GCC web site which lists which features of C++11 are supported in each version of the compiler:
v4.3
v4.4
v4.5
v4.6
v4.7
Comparing the v4.6 and v4.7 page, one easy-to-test feature where support was first added in v4.7 is delegating constructors. So I wrote up a quick test, which
I used to confirm the difference between the two compilers:
$ sudo update-alternatives --config gcc # select the old v4.6 compiler$ g++ -std=c++0x 2011-12-24_GCCv47.cpp2011-12-24_GCCv47.cpp: In constructor ‘A::A()’:2011-12-24_GCCv47.cpp:36:2:
error: type ‘A’ is not a direct base of ‘A’$ sudo update-alternatives --config gcc # select the new v4.7 compiler$ g++ -std=c++11 2011-12-24_GCCv47.cpp$ ./a.outa.value == 3
How to install and use GCC g++ v4.7 and C++11 on Ubuntu 12.04 (beta)
2011-12-24_GCCv47.cpp
Summary
I had a bit of extra spare time over the 2011 holiday season, and I wanted to test out some of the new C++11 features. But I know the old GCC C++ compiler I have installed in my usual LTS Ubuntu 10.04 development environment is out-of-date, so the firstthing I needed to do is upgrade my OS and GCC to something decent. Here is how I accomplished this task.
Ubuntu 12.04 (beta)
First thing was to change my operating system. From Canonical, I downloaded the latest daily live .iso file. Here is a decent place to start looking:http://cdimage.ubuntu.com/daily-live/current/precise-desktop-amd64.iso.These live-cd images are a breeze to install using VirtualBox, and within 30 minutes I had a decent development environment installed.
Installing GCC v4.6
When Ubuntu 12.04 is first installed, it defaults to GCC v4.6. And just as important, it only includes the 'C' compiler, not the 'C++' compiler:$ which g++ gcc/usr/bin/gcc$ gcc --versiongcc (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2
A number of development tools are referenced by the meta-package named build-essential, so I started by making sure this package is installed:
$ sudo apt-get install build-essential
This gave me the expected GCC v4.6 C++ compiler:
$ which g++ gcc/usr/bin/g++/usr/bin/gcc$ g++ --versiong++ (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2
Installing GCC v4.7
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
Now take a look to see if we can find the GCC packages:
$ sudo apt-cache search "g\+\+"...g++ - GNU C++ compilerg++-multilib - GNU C++ compiler (multilib files)g++-4.4 - GNU C++ compilerg++-4.4-multilib - GNU C++ compiler (multilib files)g++-4.5 - The GNU C++ compilerg++-4.5-multilib - The GNU C++ compiler (multilib
files)g++-4.6 - GNU C++ compilerg++-4.6-multilib - GNU C++ compiler (multilib files)g++-4.7 - GNU C++ compilerg++-4.7-multilib - GNU C++ compiler (multilib files)...
More information on the multilib packages can be found here, though most people shouldn't need it.
Install the GCC v4.7 C/C++ compilers with the following command:
$ sudo apt-get install gcc-4.7 g++-4.7
Switching compilers
Installing the v4.7 compilers does not remove the previous v4.6 compiler. This is very important, if you skipped the previous sentence, now is a good time to go back and re-read it! Thesystem 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.6sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7sudo 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++ --versiong++ (Ubuntu/Linaro 4.7-20111222-0ubuntu1) 4.7.0 20111222 (experimental) [trunk revision 182617]
Testing compilers
The only thing left is to test if the new v4.7 compiler is working as expected. There are several convenient pages on the GNU GCC web site which lists which features of C++11 are supported in each version of the compiler:v4.3
v4.4
v4.5
v4.6
v4.7
Comparing the v4.6 and v4.7 page, one easy-to-test feature where support was first added in v4.7 is delegating constructors. So I wrote up a quick test, which
I used to confirm the difference between the two compilers:
$ sudo update-alternatives --config gcc # select the old v4.6 compiler$ g++ -std=c++0x 2011-12-24_GCCv47.cpp2011-12-24_GCCv47.cpp: In constructor ‘A::A()’:2011-12-24_GCCv47.cpp:36:2:
error: type ‘A’ is not a direct base of ‘A’$ sudo update-alternatives --config gcc # select the new v4.7 compiler$ g++ -std=c++11 2011-12-24_GCCv47.cpp$ ./a.outa.value == 3
相关文章推荐
- How to install gcc-arm-linux-gnueabihf on Ubuntu 12.04
- How To Install and Secure phpMyAdmin on Ubuntu 12.04(MySQL图形管理)
- How to Install and Use Python Pip on Ubuntu 14.04 and pip install pytz
- How To Install and Use Memcache on Ubuntu 14.04
- How to Install Python 3.3 on Ubuntu 13.04, 12.10 and 12.04
- Ubuntu 14.04安装PostgreSQL How To Install and Use PostgreSQL on Ubuntu 14.04
- HOW TO INSTALL SUBLIME TEXT 2 ON UBUNTU 12.04 (UNITY)
- How to Install GCC 4.8 via PPA in Ubuntu 12.04, 13.04, 10.04
- Howto Install and Configure PCL on Ubuntu
- How to install and configure Apache, PHP, MySql and phpMyadmin on ubuntu
- How to Install and Configure VNC on Ubuntu 14.04
- How to install Maven 3 on Ubuntu 12.04/12.10/13.04 by using apt-get
- How To Install Wordpress with nginx on Ubuntu 12.04
- How to display Computer,Home,Network,Trash and Mounted volumes icons on ubuntu 12.04 (Precise) deskt
- How to Configure, Install and Use libnefilter_queue on Linux
- How to debug SceneLib with Eclipse and How to install SceneLib 1.0 on Ubuntu 10.04
- How to Install and Run Network Simulator (NS2) v2.35 on Ubuntu 10.04
- How To Install Elasticsearch, Logstash, and Kibana (ELK Stack) on Ubuntu 14.04
- How to install Aptana Studio 3 on Ubuntu 12.04 LTS (Precise Pangolin)