7

I'm running debian wheezy and wanted to upgrade from GCC 4.7.2 to GCC 4.9.0.

As per these instructions I installed libgmp-dev, libmpfr-dev and libmpc-dev (my package manager gave me versions 2:5.0.5+dfsg-2, 3.1.0-5 and 0.9-4 respectively) and ran the following to compile gcc (note that in my case it was 4.9.0 instead of 4.6.2):

tar xzf gcc-4.6.2.tar.gz
cd gcc-4.6.2
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-4.6.2/configure --prefix=$HOME/gcc-4.6.2 
make
make install

I now have a objdir directory full of stuff, but where is g++, and where should I put this directory?

I'm guessing I should move it to usr/local and add something to my PATH variable, but I don't know what exactly, and how to make sure it is searched before my old gcc install.

quant
  • 21,507
  • 32
  • 115
  • 211
  • No `gcc-4.9.0` directory with the binaries showed up in your home directory? – Joachim Isaksson Apr 26 '14 at 14:21
  • a lot of things are very interesting: 1) Why you install a 4.9.0 in a 4.6.2 directory? :-) 2. Why you use $PWD/../xyz instead of ../xyz ? You will find the executables in $HOME/gcc/bin/ – Klaus Apr 26 '14 at 15:47
  • @Klaus all those things are already explained. I installed it to 4.9.0 and I don't really know why they used `$PWD` but it works fine. Can I somehow make sure that when I type `g++` it points to my new install instead of the gcc that came with debian? – quant Apr 27 '14 at 04:41
  • 1
    Depending on your shell (bash?) you can edit the ~/.bashrc file. Add a new line at the end: `export PATH=//gcc-/bin:$PATH`. Now you start a new shell and type `gcc --version`. This should be the new one. – Klaus Apr 27 '14 at 08:39
  • N.B. you only need to *either* install the `libgmp-dev`, `libmpfr-dev` and `libmpc-dev` packages *or* run the `contrib/download_prerequisites` script. I thought the GCC wiki page made that clear: _"**Alternatively**, after extracting the GCC source archive, simply run the `./contrib/download_prerequisites` script in the GCC source directory."_ – Jonathan Wakely May 06 '14 at 12:47
  • Also, when using the new `g++` make sure you read and understand http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths – Jonathan Wakely May 06 '14 at 12:50

1 Answers1

7

After doing these commands (note the --prefix option of configure)

$PWD/../gcc-4.9.0/configure --prefix=$HOME/gcc-4.9.0 
make install

the new gcc will be installed in $HOME/gcc-4.9.0 directory (there should be subdirectories like bin, lib, share inside it).

Full path to gcc will be $HOME/gcc-4.9.0/bin/gcc, and g++ (which is symlink to gcc) will be here: $HOME/gcc-4.9.0/bin/g++.

There can be no separate g++ in objdir because gcc compiler driver implements drivers for several languages; the mode (C or C++) is selected based on argv[0] (name of binary, which was used to run driver: gcc or g++; this mode also affects linking flags) and on source file extensions (gcc accepts both C and C++ programs as input for compilation into object files). So, g++ may be generated as symlink by make install, and the place to store generated symlink is $prefix/bin.

After building the GCC and installing it in the $HOME/gcc-4.9.0 directory, you can use it; but default system gcc will be not updated. Update of distributive gcc should be done via distributive tools (apt, dpkg-build, etc). Current prebuild version of system-wide gcc for Wheezy is 4.7.2, 4.8.2 for Jessie and Sid and 4.9-2 for "Experimental".

osgx
  • 90,338
  • 53
  • 357
  • 513
  • So I can't replace the `g++` command to somehow point to this new binary? Will I have to manually point to that directory every time I want to run g++? – quant Apr 27 '14 at 04:43
  • you can change your own $PATH environment variable, adding new directory to it to the left of `/usr/bin`. For example, is you use bash, add to the `~/.bashrc` file: `export PATH=~/gcc-4.9.0/bin:$PATH`. Then open new terminal to check. – osgx Apr 27 '14 at 10:14
  • @osgx: Thanks for the detailed information about installing and using gcc-4.9.0. I am able to install and use the latest gcc/g++ on my GNU/Linux terminal the way the steps has been mentioned in this SO post. However I would like to know whether I can safely delete $HOME/objdir directory? It has around 3.3GB size on my machine. – Mantosh Kumar May 03 '14 at 00:15
  • Mantosh Kumar, Yes, after installing (`make install`) you can delete the directory where the project was built, there is no dependence on it. The only useful information here is "`config.log`" file (it has copy of all `configure` options and list of actually enabled features) and list of directories (`ls -l`, because it is possible to merge gcc, gdb, binutils into single srcdir/objdir and use one `configure` to get them all). Personally I always save `config.log` and file list from all build dirs (objdirs), they helps remaking the project (e.g. when next minor version of gcc is available). – osgx May 03 '14 at 01:03