Tuesday, September 10, 2013

Install gcc-4.8 in Ubuntu 12.04


Install the latest version of GCC compiler in Linux ( Ubuntu 12.04, CentOS, ArchLinux, etc)

by Shi, Oct 2

At the time, the default version of GCC compiler for newly installed Ubuntu 12.04 desktop is 4.6.3, although gcc-4.8 is already released.

This article is to teach you how to upgrade your gcc compiler to the lasted version 4.8 on Ubuntu 12.04.

Before you do anything, make sure you have configured correctly the network settings so that your system is connected to the internet for the upcoming update operations that are required to accomplish this task.

Remind: If you are using Ubuntu/Centos Server version, you may need to do the following two things to make sure your system be able to connect to the internet:
(Note: for Desktop version, it is usually much easier to configure the network settings, and most of time you don't need to configure it by yourself.)

(1). First, you need to configure VirtualBox settings for the network connection types.

Below is using Bridged Adapter mode for network connection type. It is faster as it treat the VM as an individual host within the network.

Open Virtual Box and right click on your CentOS VM, and click on “Settings“.



Then click on the Network in the left side, choose "Bridged Adapter" in Attached to label. and then  choose a wireless adapter to the Name label, I choosed "Inter(R) Centrino(R) Advance-N 6205. In Advanced options, make sure "Cable Connected" is checked. After this done, click on OK.

Now you have configured the VirtualBox, start your operation system (Ubuntu 12.04)  in VirtualBox.

(2). Second, configure the network settings in Ubuntu 12.04 Server Version.

Since Debian and Ubuntu guests udev assigns a new eth number each time you give a new MAC address (in VirtualBox guest settings) , so you need to edit /etc/udev/rules.d/70-persistent-net.rules or simply delete the file(Note: it will be recreated at next boot)

if you use the below two commands, you should be able to connect to the internet now:

sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
sudo reboot


Simply type: ping www.google.com to test whether the connection is successful.

The newest version of GCC: As of 6/05/2013, there is currently a release of gcc 4.8.1 for 12.04(precise) available athttps://launchpad.net/~ubuntu-toolchain-r/+archive/test.

Now we are going to explore in how to update GCC to the latest version
There are two major ways to do this:

1. Use the toolchain/test PPA 
2. Complie the gcc source files and make install

1. Use the third party toolchain/test PPa to install gcc

Background:
The announcement for the release of gcc-4.8 was made on 3/22/2013, so you may not able to see it in an official repository site. However, it appears that Launchpad does have a PPA available for the toolchain test builds that does include gcc-4.8, here: https://launchpad.net/~ubuntu-toolchain-r/+archive/test

According to the changelog and package list, there are debs for i386, amd64, arm, and powerpc and were built against raring 13.04.

The latest available version of gcc, for 12.04, is 4.8.1 and is available in the toolchain PPA.

Steps:
(1) Adding the toolchain/test PPA:
To add the PPA to your system, open a terminal(Ctrl+Alt+tand run the following commands:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
If you try to get the latest g++, just change the above gcc to g++,
for example:
sudo apt-get install g++-4.8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

(2) Link to the standard location of library
Errors:**************************************************************************
sometimes when you write programs that need to include headers in standard directory /usr/include/sys/.. like:
#include<sys/types.h>
#include<sys/stat.h>

Ubuntu may pops you an error message saying "No such file or directory", and you will find, there is no sys directory under include directory, like below:


The problem is because by default only the 64 bit libraries are included and all the header files go into /usr/include/x86_64-linux-gnu directory(although the header files are same for 32 bit and 64 bit version of Ubuntu). These directories are part of debian package lib6-dev. If you want the 32 bit libraries then you need to install the package libc6-dev-i386. This package creates /usr/include/sys along with other ones.

However, the headers are soft linked to /usr/include/x86_64-linux-gnu/sys.

sudo apt-get install libc6-dev-i386
*********************************************************************************

(3) Compile C & C++ source code using new version of gcc/g++ compiler?
To compile .cpp code, You need to add an option to specify g++4.8, that is, add an option " -std=c++11 "

The reason is that this is specific of GCC, you have to specify it explicitly. If you are using some IDE, you may be able go to project options and specify -std=c++11 there quit simply. Otherwise if you are compiling c or c++ source code in command line you will have to add the above option every time you compile the code.

Here are some solutions to avoid typing "-std=c++11" option every time you compile the code:
1. The simplest way is to wrap it in an alias. what you may need to do is add the following to your .bashrc file, and it should work perfectly.


alias g++='g++ -std=c++11'
2. Also, do it in a make file, or you can do something like this in a two-liner/usr/local/bin/g++:
#!/bin/bash
/usr/bin/g++ -std=c++11 "$@"
which, by appearing earlier in the path, will "win" over the default g++ binary and call it for you.
Consequently, your code may become non-portable. This is trade-off.

2. Building GCC-4.8 from source

If you need gcc-4.8 on 12.04 now, your only option is to build it from source.
Please read the GCC installation FAQ prior to installation.

You can download gcc-4.8 from one of gnu.org's mirror sites or directly from their SVN server.

Here is an example of steps to compile from source (see here for additional details.) Note that these may vary depending on your system and preferences.
Download the source code
Steps:
(1) Download the source file:
sudo wget http://www.netgull.com/gcc/releases/gcc-4.8.2/gcc-4.8.2.tar.bz2
(adjust above address to an appropriate mirror site. )
Unzip the file (tar -jxvf <file name>)  to a temporary directory
(2) Install some additional libraries
sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev libc6-dev
(3) Enter the source file directory, compile the source:
./gcc-4.8.0/configure --prefix=/usr/gcc/
make && make install 
Once this process has completed, run the command gcc --version to verify that the installation has completed successfully. You should see something similar to the following output:

shi@virtualbox:~$ gcc --version
gcc (Ubuntu 4.8.1-2ubuntu1~12.04)  4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Note: Don't forget to add /usr/bin/gcc/ into your system PATH

1 comment:

  1. For ubuntu 12.04 server version, if you want to put full screen in virtualbox, perhaps the below method will get you go:

    #Add kernel option for 1024x768 resolution to GRUB
    sudo sed -i "s/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"vga=0x0305 /" /etc/default/grub
    #Run update-grub for the new setting to take effect
    sudo update-grub

    ReplyDelete