Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an older version of gcc in Linux

In Linux I am trying to compile something that uses the -fwritable-strings option. Apparently this is a gcc option that doesn't work in newer version of gcc. I installed gcc-3.4 on my system, but I think the newer version is still being used because I'm still get the error that says it can't recognize the command line option -fwritable-strings. How can I get make to use the older version of gcc?

like image 269
node ninja Avatar asked Sep 13 '10 07:09

node ninja


People also ask

Does GCC work on Linux?

The GNU Compiler Collection, commonly known as GCC, is a set of compilers and development tools available for Linux, Windows, various BSDs, and a wide assortment of other operating systems. It includes support primarily for C and C++ and includes Objective-C, Ada, Go, Fortran, and D.


2 Answers

You say nothing about the build system in use, but usually old versions of gcc can be invoked explicitly, by something like (this is for an autotools-based build):

./configure CXX=g++-3.4 CC=gcc-3.4

For a make-based build system, sometimes this will work:

make CXX=g++-3.4 CC=gcc-3.4

Most makefiles ought to recognise overriding CC and CXX in this way.

like image 63
Jack Kelly Avatar answered Sep 24 '22 07:09

Jack Kelly


If editing the configuration/Makefile is not an option, Linux includes a utility called update-alternatives for such situations. However, it's a pain to use (links to various tutorials included below).

This is a little simpler - here's a script (from here) to easily switch your default gcc/g++ version:

#!/bin/bash 
usage() {
        echo 
        echo Sets the default version of gcc, g++, etc
        echo Usage:
        echo 
        echo "    gcc-set-default-version <VERSION>"
        echo 
        exit
}
cd /usr/bin
if [ -z $1 ] ; then 
        usage;
fi 
set_default() {
        if [ -e "$1-$2" ] ; then 
                echo $1-$2 is now the default
                ln -sf $1-$2 $1
        else 
                echo $1-$2 is not installed
        fi
}
for i in gcc cpp g++ gcov gccbug ; do 
        set_default $i $1
done

If you 1) name this script switch-gcc, 2) put it in your path, and 3) make it executable (chmod +x switch-gcc), you can then switch compiler versions just by running

sudo switch-gcc 3.2

Further reading on update-alternatives:

  • https://lektiondestages.blogspot.com/2013/05/installing-and-switching-gccg-versions.html
  • https://codeyarns.com/2015/02/26/how-to-switch-gcc-version-using-update-alternatives/
  • https://askubuntu.com/questions/26498/choose-gcc-and-g-version
like image 29
Aaron V Avatar answered Sep 21 '22 07:09

Aaron V