First, since you have run
sudo apt-get install build-essential you must be having
gcc installed already. You can find out which version you have by running
The location of this installed version would be
/usr/bin/gcc is the default location of the binary of
gcc in Ubuntu.
You want to install a previous version.
Since this version is available in the older releases of Ubuntu, we need to have the appropriate repositories for the version. From some search, I found them to be the following, which have to be added to the
deb http://snapshot.debian.org/archive/debian/20070730T000000Z/ lenny main
deb-src http://snapshot.debian.org/archive/debian/20070730T000000Z/ lenny main
deb http://snapshot.debian.org/archive/debian-security/20070730T000000Z/ lenny/updates main
deb-src http://snapshot.debian.org/archive/debian-security/20070730T000000Z/ lenny/updates main
*These links have been taken from this answer.
Then after doing
sudo apt-get update the new repositories will be available.
Next, install the required compiler. I usually install
g++ of the same version for inter-operability. For general purpose it is recommended.
sudo apt-get install gcc-4.1 g++-4.1
2. Checking the available compilers
At this stage one will have two set of compilers (oen each for
gcc). These can be checked by
dpkg --list | grep compiler,
dpkg --list | grep compiler
ii antlr3.2 3.2-14 all language tool for constructing recognizers, compilers etc
ii g++ 4:5.3.1-1ubuntu1 amd64 GNU C++ compiler
ii g++-4.1 4.1.2-13 amd64 The GNU C++ compiler
ii g++-4.1-multilib 4.1.2-13 amd64 The GNU C++ compiler (multilib files)
ii g++-5 5.4.0-6ubuntu1~16.04.4 amd64 GNU C++ compiler
ii gcc 4:5.3.1-1ubuntu1 amd64 GNU C compiler
ii gcc-4.1 4.1.2-13 amd64 The GNU C compiler
ii gcc-4.1-multilib 4.1.2-13 amd64 The GNU C compiler (multilib files)
ii gcc-5 5.4.0-6ubuntu1~16.04.4 amd64 GNU C compiler
ii gcc-5-multilib 5.4.0-6ubuntu1~16.04.4 amd64 GNU C compiler (multilib support)
ii gcc-multilib 4:5.3.1-1ubuntu1 amd64 GNU C compiler (multilib files)
ii hardening-includes 2.7ubuntu2 all Makefile for enabling compiler flags for security hardening
ii libantlr-java 2.7.7+dfsg-6ubuntu1 all language tool for constructing recognizers, compilers etc (java library)
ii libllvm3.8:amd64 1:3.8-2ubuntu3 amd64 Modular compiler and toolchain technologies, runtime library
ii libllvm3.8:i386 1:3.8-2ubuntu3 i386 Modular compiler and toolchain technologies, runtime library
ii libxkbcommon0:amd64 0.5.0-1ubuntu2 amd64 library interface to the XKB compiler - shared librar
Important is the location of the two set of compilers, which can be listed by,
ls -lh /usr/bin/gcc*
lrwxrwxrwx 1 root root 5 五 5 2016 /usr/bin/gcc -> gcc-5
-rwxr-xr-x 1 root root 208K 七 10 2007 /usr/bin/gcc-4.1
-rwxr-xr-x 1 root root 895K 十一 3 10:00 /usr/bin/gcc-5
ls -lh /usr/bin/g++*
-rwxr-xr-x 1 root root 210K 七 10 2007 /usr/bin/g++-4.1
-rwxr-xr-x 1 root root 899K 十一 3 09:41 /usr/bin/g++-5
- Selecting a compiler for current purpose (building an application)
After having required compilers installed one can simply switch among compilers. This is done by updating the list of alternative versions of an application. To do this, the
update-alternative command has to be run with certain parameters.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.1 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.1
sudo update-alternatives --config gcc
This will link
gcc and on changing only
g++ will change automatically.
Then whenever you want to change the compiler enter this:
sudo update-alternatives --config gcc
Then, user is asked which compiler to choose.
Selection Path Priority Status
* 0 /usr/bin/gcc-5 60 auto mode
1 /usr/bin/gcc-4.1 40 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Here you can choose by pressing the key(0,1) and then pressing enter. The change in the current selected version can be checked by
4. Building an application using installed compilers
Some basic guidelines:
- There are in general two steps, building (
build) and making (
build step involves the generation of system specific codes for installation which requires linking to required compilers.
make is equivalent to installation in some directory. Any application can be
make in any folder as decided by the user.
build a temporary folder is chosen. This folder can be removed after a successful
make step. Important is that for the
make step the installation destination is defined usually by
--prefix=/path/to/folder while the
build script is run. For example, if the setup script is
.setup then, in most cases,
For specific details please refer to the manual or guidelines for your application.
When running the build script the current selected compiler version (for
g++) will be used. When running the installed application, the behavior will depend on the case whether the program takes compiler from environment variable or is linked to the specific binary. That has to be checked after installation.
make are separate utility used for installation. Different applications require different versions, so install the required utility and version accordingly.
The step of
make are same for compilers as well.