4

I would like to use an old version of gcc for one of my program (versions 3.* would be good), any idea how to do this?

David Buck
  • 3,752
  • 35
  • 31
  • 35
aurel_lab
  • 149
  • 11

2 Answers2

1

Just compile and install it somewhere and optionally add its location to your $PATH. Do this in a directory where you downloaded gcc source code:

$ contrib/download_prerequisites
$ cd ..
$ mkdir objdir
$ cd objdir
$ ../gcc/configure --enable-languages=c --disable-multilib --prefix=$HOME/gcc-4.6.2 # modify option to suit your needs
$ make -j8
$ make install

Run it in $HOME/gcc-4.6.2:

$ usr/local/bin/gcc --version

(or use make install DESTDIR=<DIR> instead of --prefix)

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • Thank you for your answer, but where can i find gcc source code for a old version ? – aurel_lab Feb 07 '16 at 12:35
  • Also, i don't understand the first lines of your solution: what is contrib/download_prerequisites ? why do you go back with cd.. ? why do you call configure outside of the folder ? – aurel_lab Feb 07 '16 at 12:44
  • You can download current and old `gcc` releases from one of the servers listed [here](https://gcc.gnu.org/mirrors.html). Use your favorite ftp client or a web browser and pick a server that's closest to your place. – Arkadiusz Drabczyk Feb 07 '16 at 14:10
  • As said [here](https://gcc.gnu.org/wiki/InstallingGCC): `do not run ./configure, this is not supported, you need to run configure from outside the source directory (this is a FAQ)` – Arkadiusz Drabczyk Feb 07 '16 at 14:10
1

If you are trying to install an older version of GCC that is no longer available (i.e. gcc 4.9) on ubuntu, you will not be able to download it directly.

Open your /etc/apt/sources.list file and append the following two lines:

deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe

If this is your first time installing versioning on GCC run:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

Then in your terminal run the installation for any versions you are attempting to add. For example, if you want to install 4.9 (outdated) alongside gcc 7 and gcc 8 (not outdated):

Sources: How to install GCC Compiler on Ubuntu 18.04I need to install gcc4.9 on ubuntu 20.04|matlab mex

sudo apt update
sudo apt install gcc-4.9 g++ 4.9 gcc-7 g++-7 gcc-8 g++-8

Next, configure the GCC alternatives and their priority level:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 49 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8

To compile your code using one of the newly installed versions, simply specify at compile time:

g++-4.9 <file_name>.cpp

To set your default GCC compiler use:

sudo update-alternatives --config gcc

Note: If this is your first time installing any version of GCC you may need to run

sudo apt install build-essential
sudo apt-get install manpages-dev

Sources: How to install GCC Compiler on Ubuntu 18.04 I need to install gcc4.9 on ubuntu 20.04|matlab mex