Manually Build and Install GNU Compiler Collection on Linux Mint

The GNU C/C++ Compiler Collection installed by default in Linux Mint is usually not the most up to date compiler on your system. If you want to try new functions of the latest version of GCC, you need to build it. C++ programmers will be especially interested in building version 4.8.1 because: "Support for C++11 ref-qualifiers was added to the GCC 4.8 branch, making G++ the first C++ compiler to implement all the major language features of the C++11 standard. This functionality will be available in GCC 4.8.1."

Disk space requirement: 500MB for the source and ~2.1GB for the compiled files. About 3GB space will be needed for the temporary files when compiling.

Steps

  1. Install required packages
    • Type/Copy/Paste: sudo apt-get install binutils build-essential m4 autogen bison flex
    • Type/Copy/Paste: sudo apt-get install g++
    • You will need these packages as part of a complete build environment.
  2. Open up a terminal and type the following:
    • Type/Copy/Paste: mkdir /home/"your_user_name"/gcc_archive
    • This will create a directory to hold your downloaded packages and compiled libraries
  3. Download the following programs and place them in the gcc_archive directory
    • You will download all your packages you need to compile GCC to the /home/"your_user_name_"/gcc_archive directory. So change into the directory.
    • Type/Copy/Paste: cd /home/"your_user_name"/gcc_archive
  4. While in the /home/"your_user_name"/gcc_archive directory run the following commands.
  5. Install GNU GMP Multiprecision Arithmetic Library, for arbitrary precision arithmetic:
    • Go to ftp://ftp.gmplib.org/pub/ to download the latest version, then run these commands in the folder contains that file to extract, build and install GMP:
    • Type/Copy/Paste: cd /home/"your_user_name"/gcc_archive
    • Type/Copy/Paste: sudo mkdir /opt/gmp-5.1.3
    • Type/Copy/Paste: chmod a+x gmp-5.1.3.tar.bz2
    • Type/Copy/Paste: tar -jxvf gmp-5.1.3.tar.bz2
    • Type/Copy/Paste: cd gmp-5.1.3
    • Type/Copy/Paste: ./configure --prefix=/opt/gmp-5.1.3
    • Type/Copy/Paste: make && make check && sudo make install
    • Notes: It is recommended to run make check
    • Type/Copy/Paste: cd ..
  6. Install GNU MPFR Library, Multiple-Precision Floating-Point computations with correct rounding
    • Go to http://www.mpfr.org/mpfr-current/ to download the latest version, then run these commands in the folder contains that file to extract, build and install MPFR:
    • Type/Copy/Paste: cd /home/"your_user_name"/gcc_archive
    • Type/Copy/Paste: sudo mkdir /opt/mpfr-3.1.2
    • Type/Copy/Paste: chmod a+x mpfr-3.1.2.tar.bz2
    • Type/Copy/Paste: tar -jxvf mpfr-3.1.2.tar.bz2
    • Type/Copy/Paste: cd mpfr-3.1.2
    • Type/Copy/Paste: ./configure --prefix=/opt/mpfr-3.1.2 --with-gmp=/opt/gmp-5.1.3
    • Type/Copy/Paste: make && make check && sudo make install
    • Notes: You must install GMP before installing MPFR
    • Type/Copy/Paste: cd ..
  7. Install GNU MPC Library, for arithmetic using complex numbers
    • Go to http://www.multiprecision.org/index.php?prog=mpc&page=download to download the latest version, then run these commands in the folder contains that file to extract, build and install MPFR:
    • Type/Copy/Paste: cd /home/"your_user_name"/gcc_archive
    • Type/Copy/Paste: sudo mkdir /opt/mpc-1.0.1
    • Type/Copy/Paste: chmod a+x mpc-1.0.1.tar.gz
    • Type/Copy/Paste: tar -zxvf mpc-1.0.1.tar.gz
    • Type/Copy/Paste: cd mpc-1.0.1
    • Type/Copy/Paste: ./configure --prefix=/opt/mpc-1.0.1 --with-gmp=/opt/gmp-5.1.3 --with-mpfr=/opt/mpfr-3.1.2
    • Type/Copy/Paste: make && make check && sudo make install
    • Notes: You must install GMP and MPFR before installing MPC
    • Type/Copy/Paste: cd ..
  8. Configure build options for GNU Compiler Collection (GCC)
    • Download the the latest version of GCC from http://ftp.gnu.org/gnu/gcc/gcc-4.8.1/gcc-4.8.1.tar.bz2 . Since the GCC website did say that we should run configure command in another folder other than the source folder, we need to create another folder just for this (e.g. gcc481_build):
    • Type/Copy/Paste: cd /home/"your_user_name"/gcc_archive
    • Type/Copy/Paste: sudo mkdir -p /opt/gcc-4.8.1
      • This will create the directory which will hold the new GNU Compiler Collection (GCC)
    • Type/Copy/Paste: chmod a+x gcc-4.8.1.tar.bz2
    • Type/Copy/Paste: tar -jxvf gcc-4.8.1.tar.bz2
    • Type/Copy/Paste: mkdir gcc481_build
      • This will create the scratch directory to build the GNU Compiler Collection (GCC)
    • Type/Copy/Paste: cd gcc481_build
    • Type/Copy/Paste: ../gcc-4.8.1/configure --prefix=/opt/gcc-4.8.1 --with-gmp=/opt/gmp-5.1.3 --with-mpfr=/opt/mpfr-3.1.2 --with-mpc=/opt/mpc-1.0.1 --disable-multilib --enable-languages=c,c++
  9. While in the gcc481_build directory, set some environment variables to prepare for the upcoming build
    • 32-bit Linux Mint Instructions:
      • Type/Copy/Paste: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gmp-5.1.2/lib:/opt/mpfr-3.1.2/lib:/opt/mpc-1.0.1/lib
      • Type/Copy/Paste: export C_INCLUDE_PATH=/usr/include/i386-linux-gnu
      • Type/Copy/Paste: export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH
      • Type/Copy/Paste: export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH
      • Type/Copy/Paste: export LIBRARY_PATH=/usr/lib/i386-linux-gnu
    • 64-bit Linux Mint Instructions:
      • Type/Copy/Paste: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gmp-5.1.2/lib:/opt/mpfr-3.1.2/lib:/opt/mpc-1.0.1/lib
      • Type/Copy/Paste: export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
      • Type/Copy/Paste: export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH
      • Type/Copy/Paste: export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH
      • Type/Copy/Paste: export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
  10. Build and install GNU Compiler Collection (GCC)
    • Type/Copy/Paste: make -j 4
    • Type/Copy/Paste: sudo make install
    • Notes: Be prepared for a long build -- it usually takes anywhere from 2 to 4 hours for a complete build, however it all depends on the speed of your processor. I would expect at least a 4 hour build/compile on most systems.
  11. Modify your system-wide environment variables to support new version of GNU Compiler Collection (GCC)
    • Since there is no default .bashrc file for users on Linux Mint, you do not need to add the PATH to the new GCC to such a file
    • Type/Copy/Paste: sudo nano /etc/profile
  12. Then add the following lines to the end of /etc/profile, then save and close it:
    • 32-bit Linux Mint Instructions:
      • Type/Copy/Paste: export PATH=/opt/gcc-4.8.1/bin:$PATH
      • Type/Copy/Paste: export LIBRARY_PATH=/usr/lib/i386-linux-gnu
    • 64-bit Linux Mint Instructions:
      • Type/Copy/Paste: export PATH=/opt/gcc-4.8.1/bin:$PATH
      • Type/Copy/Paste: export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
  13. Use a text editor such as nano or gedit and add 3 lines to your /etc/ld.so.conf
    • Type/Copy/Paste: sudo nano /etc/ld.so.conf
    • Type/Copy/Paste: /opt/gmp-5.1.2/lib
    • Type/Copy/Paste: /opt/mpfr-3.1.2/lib
    • Type/Copy/Paste: /opt/mpc-1.0.1/lib
  14. Run the command:
    • Type/Copy/Paste: sudo ldconfig -v
      • This will reload update your /etc/ld.so.conf file
  15. Update the environment variables in your open terminal by running the following command
    • Type/Copy/Paste: . /etc/profile
  16. Verify your terminal's PATH environment variable by issuing the following command:
    • Type/Copy/Paste: printenv PATH
      • This should show /opt/gcc-4.8.1/bin is now at the front of your PATH
  17. Finally, run below commands to check the gcc version. They should output the version of GCC you have just built (e.g. 4.8.1)
    • Type/Copy/Paste: gcc --version
    • Type/Copy/Paste: g++ --version
  18. After these steps are complete without error, reboot your Linux Mint operating system and then your system will configured properly for compiling GNU C/C++ programs on your Linux Mint system.
  19. Later still, after you have confirmed that the new version is working fine you will probably want to delete the contents of /home/"your_user_name"/gcc_archive to reclaim space.
  20. Also to build a truly portable GNU GCC Compiler, one that is transferable between different GNU/Linux Mint systems see the following article Manually-Build-GNU-Compiler-Collection-from-Scratch-on-Linux-Mint-Static-Edition Good Luck and as Richard Stallman likes to say "Happy Hacking !"
  21. Location for sources:
  22. Location of the GCC infrastructure:



Warnings

  • As with anything you build on Linux, you need to be alert for errors occurring during any of the build steps. Should any such errors occur, first double-check that you have all the required packages.

Related Articles

You may like