Need Help in installing latest C++ version on Ubuntu please

Pages: 12
Hello,
At present I have on my Ubuntu laptop, I have C++ gcc version 11.2.0.

I like to upgrade it to GCC 12.2 that was released on August 19, 2022.

Are there one or two URL links that give detailed steps for upgrading to GCC 12.2 on my Ubuntu laptop please?

So go to the gnu project homepage. ( https://gcc.gnu.org/ ). On the right side you will find the word download with two options. Go to Mirrors.

Pick the mirror that is closest to you geographically for best speeds (click).
Click file called "releases"
The newest version is at the bottom of this list, click that.
Click on either of the two largest files, the tar.xz has better compression, so I'd suggest that one.

In a bash terminal do the following:
tar -xvf gcc-12.2.0.tar.xz
cd gcc-12.2.0

./configure --disable-multilib
make -j4
sudo make install


You may have to call configure several times-> The last paragraph on failure is very helpful, it will tell you what dependencies are missing, so do a google search for what each package name is for Ubuntu. Get the dev lib wherever possible.
Ubuntu looks to be 64 bit only, so that's why --disable-multilib is suggested above.

I suggest that you sudo apt-get remove your old version of g++, the new version is likely to be installed in /usr/local, which gets lower path priority than where the Apt-get version is installed.

To verify your new install:
g++ --version
Last edited on
Ubuntu has package management. Software is available for install in package repositories.

The benefit of managed packages is that you can not only install, but also remove files that belong to a package and a package can specify that other packages are required before this one can be installed. These simplify the "What else do I need?" and "How do I clean out what I don't need any more?".

So, how to get GCC 12? Do maintainers of Ubuntu add new software packages to base repositories? When? Does someone offer repository for additional content that Ubuntu does not provide?


A source install does not care what it overwrites. A good habit is to give --prefix=path for the configure. A path that you can write to without sudo, i.e. you can run make install as yourself and then you know that only files of this install will be in location path.

The only downside is that you want shell to find your custom version, rather than systems version (if any). You can prepend entries to PATH in ~/.bash_profile (if you do use Bash).
Hello newbieg and keskiverto,
Thank you very much for giving detailed information.

I will follow both of yours input and install/update to GCC 12.2

Again thanks so much.

owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ pwd
/home/owner/SSJ/CPP_gpp_12_2/gcc-12.2.0
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ configure --disable-multilib
configure: command not found
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ ls
ABOUT-NLS           config-ml.in  COPYING.LIB      INSTALL       libcpp        libobjc          libvtv          maintainer-scripts  multilib.am
ar-lib              config.rpath  COPYING.RUNTIME  install-sh    libdecnumber  liboffloadmic    ltgcc.m4        Makefile.def        NEWS
build-gcc           config.sub    c++tools         intl          libffi        libphobos        ltmain.sh       Makefile.in         README
ChangeLog           configure     depcomp          LAST_UPDATED  libgcc        libquadmath      lt~obsolete.m4  Makefile.tpl        symlink-tree
ChangeLog.jit       configure.ac  fixincludes      libada        libgfortran   libsanitizer     lto-plugin      MD5SUMS             test-driver
ChangeLog.tree-ssa  contrib       gcc              libatomic     libgo         libssp           ltoptions.m4    missing             ylwrap
compile             COPYING       gnattools        libbacktrace  libgomp       libstdc++-v3     ltsugar.m4      mkdep               zlib
config              COPYING3      gotools          libcc1        libiberty     libtool-ldflags  ltversion.m4    mkinstalldirs
config.guess        COPYING3.LIB  include          libcody       libitm        libtool.m4       MAINTAINERS     move-if-change
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ 
Hello newbieg.
I have followed steps but it is saying configure is not found.
How to fix this error please?
Oops, try
./configure --disable-multilib

It's a script in the gcc folder.
I'll modify the instructions above.


P.S. I'd never looked in the folder called INSTALL, but that's the manual for all the config, build, install steps and options.
Last edited on
Debian "Testing" comes witch GCC 12.2. So does Fedora 36.

https://cdimage.debian.org/cdimage/weekly-builds/amd64/iso-dvd/
Last edited on

wner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ ./configure --disable-multilib
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for gawk... no
checking for mawk... mawk
checking for libatomic support... yes
checking for libitm support... yes
checking for libsanitizer support... yes
checking for libvtv support... yes
checking for libphobos support... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for gcc option to accept ISO C99... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking whether g++ accepts -static-libstdc++ -static-libgcc... yes
checking for gnatbind... no
checking for gnatmake... no
checking whether compiler driver understands Ada... no
checking for gdc... no
checking whether the D compiler works... no
checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2
checking for objdir... .libs
checking for the correct version of gmp.h... no
configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations. Source code for these libraries can be found at
their respective hosting sites as well as at
https://gcc.gnu.org/pub/gcc/infrastructure/. See also
http://gcc.gnu.org/install/prerequisites.html for additional info. If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files. They may be located in separate packages.
owner@owner-Lenovo:~/SSJ/CPP_gpp_12_2/gcc-12.2.0$ make -j4
make: *** No targets specified and no makefile found. Stop.

make -j4 is giving errors please
Following URL has some information:
https://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/

To download and extract the latest GCC source code, enter the following commands in the Cygwin terminal. If you’re following this guide at a later date, there will surely be a more recent version of GCC available. I used 4.9.2, but you can use any version you like. Keep in mind, though, that it’s always best to have the latest Cygwin packages installed when building the latest GCC. Be patient with the tar command; it takes several minutes.

$ wget http://ftpmirror.gnu.org/gcc/gcc-4.9.2/gcc-4.9.2.tar.gz
$ tar xf gcc-4.9.2.tar.gz
That will create a subdirectory named gcc-4.9.2. Next, we’ll configure our GCC build. As the GCC documentation recommends, it’s best to configure and build GCC in another directory outside gcc-4.9.2, so that’s what we’ll do.

$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-4.9.2/configure --program-suffix=-4.9.2 --enable-languages=c,c++ --disable-bootstrap --disable-shared
Here’s a description of the command-line options passed to configure:

The --program-suffix=-4.9.2 option means that once our new GCC is installed, we’ll run it as g++-4.9.2. This will make it easier for the new GCC compiler to coexist alongside the system GCC compiler provided by Cygwin.

The --enable-languages=c,c++ option means that only the C and C++ compilers will be built. Compilers for other languages, such as Fortran, Java and Go, will be excluded. This will save compile time.

The --disable-bootstrap option means that we only want to build the new compiler once. If we don’t specify --disable-bootstrap, the new compiler will be built three times, for testing and performance reasons. However, the system GCC compiler (4.8.3) provided by Cygwin is pretty recent, so --disable-bootstrap is good enough for our purposes. This will save a significant amount of compile time.

The --disable-shared option means that we don’t want to build the new standard C++ runtime library as a DLL that’s shared with other C++ applications on the system. It’s totally possible to make C++ executables work with such DLLs, but it takes care not to introduce conflicts with C++ executables created by older or newer versions of GCC. That’s something distribution maintainers need to worry about; not us. Let’s just avoid the additional headache.

By default, the new version of GCC will be installed to /usr/local in Cygwin’s virtual filesystem. This will make it easier to launch the new GCC, since /usr/local/bin is already listed in Cygwin’s PATH environment variable. However, if you’re using an existing Cygwin installation, it might prove difficult to uninstall GCC from /usr/local later on (if you so choose), since that directory tends to contain files from several different packages. If you prefer to install the new GCC to a different directory, add the option --prefix=/path/to/directory to the above configure command.

We’re not going to build a new Binutils, which GCC relies on, because the existing Binutils provided by Cygwin is already quite recent. We’re also skipping a couple of packages, namely ISL and CLooG, which means that the new compiler won’t be able to use any of the Graphite loop optimizations.

Next, we’ll actually build the new GCC compiler suite, including C, C++ and the standard C++ library. This is the longest step.

$ make -j4
The -j4 option lets the build process spawn up to four child processes in parallel. If your machine’s CPU has at least four hardware threads, this option makes the build process run significantly faster. The main downside is that it jumbles the output messages generated during the build process. If your CPU has even more hardware threads, you can specify a higher number with -j. For comparison, I tried various numbers on a Xeon-based machine having 12 hardware threads, and got the following build times:

Be warned: I encountered a segmentation fault the first time I ran with -j4. Bad luck on my part. If that happens to you, running the same command a second time should allow the build process to finish successfully. Also, when specifying higher numbers with -j, there are often strange error messages at the end of the build process involving “jobserver tokens”, but they’re harmless.

Once that’s finished, install the new compiler:

$ make install
$ cd ..
This installs several executables to /usr/local/bin; it installs the standard C++ library’s include files to /usr/local/include/c++/4.9.2; and it installs the static standard C++ library to /usr/local/lib, among other things. Interestingly, it does not install a new standard C library! The new compiler will continue to use the existing system C library that came with Cygwin.

If, later, you decide to uninstall the new GCC compiler, you have several options:

If you installed GCC to a directory other than /usr/local, and that directory contains no other files, you can simply delete that directory.
If you installed GCC to /usr/local, and there are files from other packages mixed into the same directory tree, you can run the list_modifications.py script from this post to determine which files are safe to delete from /usr/local.
You can simply uninstall Cygwin itself, by deleting the C:\cygwin64 folder in Windows, along with its associated Start menu entry.
Please help me: make -j4 has not worked for me.

configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations. Source code for these libraries can be found at
their respective hosting sites as well as at
https://gcc.gnu.org/pub/gcc/infrastructure/. See also
http://gcc.gnu.org/install/prerequisites.html for additional info. If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files. They may be located in separate packages.
The error message means that you are missing required prerequisites to build GCC.

As pointed out before, it will probably be easier to simply upgrade to a distro that comes with newer GCC version...

https://cplusplus.com/forum/beginner/284763/#msg1234302
Last edited on
@AlexCantor,

Installing the latest GCC version is a good idea, but if you are looking for C++20 compliance even the latest version is not. Only Visual Studio is 100% compliant at this time.

GCC doesn't have module support, neither is text formatting with std::format. Two C++20 library features I use. A lot.
https://en.cppreference.com/w/cpp/compiler_support/20

There are other parts of C++20 GCC hasn't implemented even with 12.2, but those features are not something I use on a regular basis.

{Edited to add}:

There is a work-around for std::format with GCC: the {fmt} 3rd party library.
https://fmt.dev/latest/index.html

How you would install it to work with GCC I can't advise you. :)

Sadly there is no work-around for modules.
Last edited on
std::format/{fmt} is really helpful with tabular output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// using a while loop to calculate the sum of integers from 1 to n and n!

import <iostream>;
import <format>;

int main()
{
   std::cout << "This program calculates n! and the sum of the integers "
             << "up to n for values 1 to limit.\n\n";
   std::cout << "What upper limit for n would you like? ";

   unsigned int limit {};
   std::cin >> limit;
   std::cout << '\n';

   // the format string for all rows of the table
   const auto table_format { "{:>8} {:>8} {:>20}\n" };

   // output column headings
   std::cout << std::format(table_format, "integer", "sum", "factorial");

   unsigned int       n         {};
   unsigned int       sum       {};
   unsigned long long factorial { 1ULL };

   while ( ++n <= limit )
   {
      sum += n;                    // accumulate sum to current n
      factorial *= n;              // calculate n! for current n

      std::cout << std::format(table_format, n, sum, factorial);
   }
}
This program calculates n! and the sum of the integers up to n for values 1 to limit.

What upper limit for n would you like? 15

 integer      sum            factorial
       1        1                    1
       2        3                    2
       3        6                    6
       4       10                   24
       5       15                  120
       6       21                  720
       7       28                 5040
       8       36                40320
       9       45               362880
      10       55              3628800
      11       66             39916800
      12       78            479001600
      13       91           6227020800
      14      105          87178291200
      15      120        1307674368000

Since GCC doesn't do modules at this time you'd change the imports to includes.

With the {fmt} library you'd substitute <format> with <fmt/core.h> and std::format becomes fmt:format.
Last edited on
@George P,

I fully agree with you that the Visual Studio C++ 2022 is the best.
However, at my work I have to use Ubuntu Linux compiler.
That is the reason, I am learning/installing G++ 12.2.

Thanks and best regards,

You don't need to Lucy 'splain why you want to install GCC. :) I "get it."

FYI, until C++20 Visual Studio was the laggard, other C++ implementations were fully compliant before MSVC++. With C++23 implementation MSVC++ is again behind other implementations.
https://en.cppreference.com/w/cpp/compiler_support/23

C++23 may be approved as the new standard, but the actual write-up is still being drafted AFAIK.

Having different compilers available is always helpful, even with the same OS, if you want to write ISO standard C++ code. Different implementations can have different C/C++ extensions that aren't available elsewhere. That is why I have MSVC++ and MSYS2 installed.

Visual Studio is quite notorious for doing that, having C/C++ extensions that work for only for Windows.
Pages: 12