Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 2 different versions of GCC on Linux Ubuntu and force MAKE to use one of them

I'm using the last version of Ubuntu which come with the gcc 4.4.5 version. I need to recompile a program that was not written by me and that can be only compiled with an older version of gcc like the 4.0. I managed to configure this older version and used a prefix during the install process so that my old gcc version is in the /opt/gcc-4.0.1/bin. I have tried to create a symlink using ln -s /opt/gcc-4.0.1/bin/gcc gcc. But when I invoke gcc -v I still get the result gcc version 4.4.5. To compile my program which come already with a makefile, if I do make, it's still using the new version of gcc. How could I tell make to use the old version?

like image 465
blackLabrador Avatar asked Nov 11 '10 07:11

blackLabrador


People also ask

How do I use multiple GCC versions?

To switch to another GCC or G++ compiler, repeat the config process in steps two(2) and three(3) above. Select a different option to set another Compiler version that you wish to use. To affirm the changes we have made running the version command on the Terminal for each compiler. Run the G++ and GCC version command.

How do I use the lower version of GCC?

After running sudo update-alternatives --config g++ a menu of g++ versions will appear and you will be asked to select the default g++ version as follows: Press <enter> to keep the current choice[*], or type selection number: Input a selection number from the menu and press Enter .

How do I specify GCC?

Specify the installation directory for the executables called by users (such as gcc and g++ ). The default is exec-prefix /bin . Specify the installation directory for object code libraries and internal data files of GCC. The default is exec-prefix /lib .


1 Answers

Make uses some standard variables in order to determine which tools to use, the C-compiler variable is called "CC". You can set the CC variable, either directly in your Makefile

CC=/opt/gcc-4.0.1/bin/gcc

which is fine if you're working alone on it, or everyone has the same setup. Or you can pass it on the command line like so:

make CC=/opt/gcc-4.0.1/bin/gcc

the third option is set /opt/gcc-4.0.1/bin before everything else in your path (which is why it doesn't work for you, the current directory isn't in the path, so the symlink you put there will not be considered when searching)

export PATH=/opt/gcc-4.0.1/bin:$PATH

For completeness, in your symlink solution, you'd have to invoke ./gcc to get the right gcc instance, but IMHO this is probably not the best solution.

HTH

like image 152
falstro Avatar answered Sep 22 '22 13:09

falstro