in C++, gcc

How to install latest C++ compiler on Windows 10?

Hello, there fellow C++ enthusiasts. On Windows operating system, most of the times, you are stuck with the compiler that comes packaged with the IDEs that you use your C++ programs or projects. But what to do if you want to try newer versions of the C++ compiler? So, in this post I will guide you to install the current latest version of the C++ compiler, i. e., GCC 10+ which will let you can play with and write software using all the new features in the recently released versions of C++.

Installation

I want to notify you that at the time of writing of this article the latest version of C++ is C++20 and the next version, i. e., C++23 is being planned and implemented. So, let’s begin: 😉
On your Windows 10 desktop:

  • Press “window” key.
  • Type “powershell”.
  • Right click on “Windows PowerShell” icon and select “Run as administrator”.
  • You’ll then be prompted with “Do you want to allow this app to make changes to your device”. Select “Yes”.
  • Then a terminal with dark blue background will pop up.
  • Then type the given below command then press enter.
choco install -y msys2
  • After successful installation. Close the current PowerShell window and open it again in a similar way.
  • Run the same command again. This time will upgrade itself.
  • After msys2 successfully upgrades itself. Type the given below command and press enter:
refreshenv
  • After your shell environment gets refreshed. Try to execute:
pacman -Syu gcc

pacman is package-management utility for MSYS2, ArchLinux, etc.

Pacman – Wikipedia
  • If the given above command does not work then you will need to add the path to all the MSYS2 utilities (programs or commands) in your environment’s PATH variable. To do so:
    • Press the “window” key.
    • Type “env” or “environment” or “environment variables”.
    • Then press Enter to open the “Environment Variables” window.
    • Search the “PATH” variable and double click on it to edit it.
    • Select the “New” option and copy the given below path:
C:\tools\msys64\usr\bin
  • Then exit out by selecting OK and OK again.
  • Run the refreshenv command again followed by: pacman -Syu gcc
  • gcc will begin installing on your system.
  • After the installation completes, you can confirm the latest version by executing this command:
gcc -v
  • It will show the following output:
Using built-in specs.
 COLLECT_GCC=gcc
 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-msys/10.2.0/lto-wrapper.exe
 Target: x86_64-pc-msys
 Configured with: /c/_/S/gcc/src/gcc-10.2.0/configure --build=x86_64-pc-msys --prefix=/usr --libexecdir=/usr/lib --enable-bootstrap --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --with-arch=x86-64 --with-tune=generic --disable-multilib --enable-__cxa_atexit --with-dwarf2 --enable-languages=c,c++,fortran,lto --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --disable-win32-registry --disable-symvers --with-gnu-ld --with-gnu-as --disable-isl-version-check --enable-checking=release --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
 Thread model: posix
 Supported LTO compression algorithms: zlib
 gcc version 10.2.0 (GCC)
  • The bottom line shows the version of gcc that is installed on your system.

Testing

Okay, so now that we have the latest C++ compiler on our system let’s test it. Here is an example of a simple C++20 “Hello, Harsh!” program:

  • Open PowerShell or Cmd whichever you prefer since we have added all the MSYS2 utilities to our PATH so all of them are visible to PowerShell and Cmd. You will not get any “unknown command” error.
  • Then open command line text-editor: nano
nano hello.cpp
  • Type the following C++20 code:
#include <iostream>
#include <compare>

auto main() -> int {
    if ((3 <=> 2) > 0) {
        std::cout << "Hello, Harsh!";
    }
}
  • Now exit out of: nano by pressing Ctrl+X then press Enter which means save the file and exit editor.
  • Then compile your code:
g++ -std=c++20 hello.cpp -o hello
  • A binary file with name “hello” will be generated.
  • Execute the binary by typing its name and then press Enter.
  • It will show the following output:
Hello, Harsh!

So that is how you can install the latest C++ compiler on Windows 10. I occasionally keep posting such technical content so you can sign up for my newsletter to get notified. Enjoy playing with new features of C++ in your latest compiler. 😉