Building GCC on Ubuntu Linux
Posted on October 7, 2016 by Paul
26 September 2018 - Article updated for GCC 8.2
This is a short article about compiling GCC 8.2 from sources on Ubuntu 18.04 64 bits. The default version of GCC on Ubuntu 18.04 is 7.3 which is not bad, however version 8 has complete C++11, C++14 support, partial C++17 support and experimental C++2a support. GCC 8 has C11 and C++14 support enabled by default, no need to add -std=c11 or -std=c++14.
First, let’s make sure that we have an up to date system:
Now, install the default GCC toolchain with:
Next, we’ll download the GCC 8 source and prerequisites from http://gcc.gnu.org/mirrors.html:
At this point, we can configure the build. In order to keep the system clean, we will use /usr/local/gcc-8.2 for the installation folder and append the suffix -8.2 to the GCC compilers. You typically don’t want to mess the system’s default GCC because other packages may depend on the default version.
Now, we are ready to build GCC, you typically want to pass twice the number of your computer cores to the make command in order to speed up the build. I have a quad-core system, so I will use 8 parallel jobs to build GCC:
Depending on the speed of your computer the build phase could take from about 30 minutes to a few hours.
Once the above phase is finished, you can install the built GCC with:
If you want to permanently add the compilers to your system’s path, add the next two lines at the end of your .bashrc file:
Don’t know how to open .bashrc ? No problem, you can find it from your Terminal:
Paste at the end .bashrc the above export lines, save the file and close gedit. Now, you just need to instruct Bash to reload .bashrc (this is automatically done when you restart your machine):
Time to test our shiny new compilers. Open your favorite text editor and copy the next piece of code (I assume you’ll save the file as test_lambda.cpp):
The code uses a generalized lambda (we could use auto for the type of the parameters), this was introduced in the C++14 standard. We can compile and test the above program with:
As mentioned at the beginning of this article, GCC 8 supports C++17. Let’s test an example of using C++17 modification to static_assert:
You can compile the above code if you pass std=c++17 to the compiler and you should get a compiler error triggered by lines 14 and 20 from the above code
If you are a Fortran programmer, you can use some of the Fortran 2008 features like do concurrent with gfortran-8.2:
The above code can be compiled with (supposing you’ve named it test_concurrent_do.f90s):
If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.
or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 4th edition:
If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen: