Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (2023)

11 mins read

Follow these steps to install g++ (the GNU C++ compiler) for Windows.There is no room for creativity here; you must follow the directions exactly.

  1. Pick the drive and a folder in which you want to install g++. I’ll assume that it isC:, but you can choose a different one.If you choose a different drive or a different folder, you’ll need to adapt the directions below accordingly.
  2. Downloadfull.exe, an about 14 megabyte executable, toC:\full.exeby right-clicking on the link. UseSave Link As…orSave Target As…Be sure the browser saves the file asC:\full.exe.
  3. Run the downloaded executable. This will install g++ (and a lot of other things that you don’t really need) on your hard drive. Go to theC:drive using Windows Explorer and double-click onfull.exe. Or, open a DOS command prompt window(Start > Programs > Command Prompt), connect to theC:drive using thecdcommand, and typefull.
  4. Locate where thebinfolder was created for the g++ installation. On my Windows machine, it was created in the following path: C:\cygnus\cygwin-b20\H-i586-cygwin32\bin You now should add it to thePATHenvironment variable. You do that by following: Start -> Control Panel -> System -> Advanced -> Environment Variables At this point you can see thePATHvariable either in theUser Variablesor in theSystem Variables. Add the g++ path into thePATHvariable. You add it to the end of theexistingvalue separated by a semicolon (‘;’). Make sure that you do not lose the original value. You are just appending more to the end separated by a semicolon.
  5. Restart your computer.A Cygnus Solutions entry will appear in your Programs menu, and an icon may appear on your desktop.Don’t use them!You will use it using theg++command on a DOS command prompt as explained below.

You should now be able to run g++ from a DOS command prompt window. For example, to compile a file calledC:\mine\hello.cpp, connect to theC:\minefolder and enter

g++ -g hello.cpp -o hello -lm

You’ll then be able to run the compiled program by enteringhelloin the DOS command prompt window.

If you’ve installed Emacs as describedhere, you will also be able to run g++ from Emacs. If, when you do this, Emacs tries to compile with the commandmake -k, you made a mistake during the Emacs installation. (If you try to compile a C program, e.g.,hello.cinstead of a C++ program, e.g.,hello.cpp, it will try tomake -k. In that case enter the compile command,gcc -g hello.c -o hello -lm, manually. After you try it manually once, from the second time on it will do the right thing automatically.) If you want to learn how to run g++ on emacs, seehere.

If you’d like to learn more about where this free compiler came from, we downloaded it from an older site ofhttp://sourceware.org/cygwin/.

If you wish to clean up a little, you may delete the file:full.exeat this point. Your g++ compiler is installed underC:\cygnus.

Several modern C++ features are currently missing from Visual Studio Express, and from the system GCC compiler provided with many of today’s Linux distributions.Generic lambdas– also known aspolymorphic lambdas– are one such feature. This feature is, however, available in the latest versions of GCC and Clang.

The following guide will help you install the latest GCC on Windows, so you can experiment with generic lambdas andother cutting-edge C++ features. You’ll need to compile GCC from sources, but that’s not a problem. Depending on the speed of your machine, you can have the latest GCC up and running in as little as15 minutes.

The steps are:

  1. Install Cygwin, which gives us a Unix-like environment running on Windows.
  2. Install a set of Cygwin packages required for building GCC.
  3. From within Cygwin, download the GCC source code, and build and install it.
  4. Test the new GCC compiler in C++14 mode using the-std=c++14option.

You can also install native GCC compilersfrom the MinGW-w64 projectwithout needing Cygwin.

1. Install Cygwin

First, download and run either the 32- or 64-bit version of theCygwin installer, depending on your version of Windows. Cygwin’s setup wizard will walk you through a series of steps. If your machine is located behind a proxy server, make sure to check “Use Internet Explorer Proxy Settings” when you get to the “Select Your Internet Connection” step.

When you reach the “Select Packages” step (shown below), don’t bother selecting any packages yet. Just go ahead and click Next. We’ll add additional packages from the command line later.

Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (1)

After the Cygwin installer completes, it’s very important to keep the installer around. The installer is an executable named eithersetup-x86.exeorsetup-x86_64.exe, and you’ll need it to add or remove Cygwin packages in the future. I suggest moving the installer to the same folder where you installed Cygwin itself; typicallyC:\cygwinorC:\cygwin64.

Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (2)

If you already have Cygwin installed, it’s a good idea to re-run the installer to make sure it has the latest available packages. Alternatively, you can install a new instance of Cygwin in a different folder.

2. Install Required Cygwin Packages

Next, you’ll need to add several packages to Cygwin. You can add them all in one fell swoop. Just open a Command Prompt (in Windows), navigate to the folder where the Cygwin installer is located, and run the following command:

C:\cygwin64>setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel
Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (3)

A window will pop up and download all the required packages along with their dependencies.

At this point, you now have a working GCC compiler on your system. It’s not the latest version of GCC; it’s whatever version the Cygwin maintainers chose as their system compiler. At the time of writing, that’s GCC 4.8.3. To get a more recent version of GCC, you’ll have to compile it yourself, using the GCC compiler you already have.

3. Download, Build and Install the Latest GCC

Open a Cygwin terminal, either from the Start menu or by runningCygwin.batfrom the Cygwin installation folder.

Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (4)

If your machine is located behind a proxy server, you must run the following command from the Cygwin terminal before proceeding – otherwise,wgetwon’t work. This step is not needed if your machine is directly connected to the Internet.

export http_proxy=$HTTP_PROXY https_proxy=$HTTP_PROXY ftp_proxy=$HTTP_PROXY

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 amore recent versionof 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 thetarcommand; it takes several minutes.

wget http://ftpmirror.gnu.org/gcc/gcc-4.9.2/gcc-4.9.2.tar.gztar xf gcc-4.9.2.tar.gz

That will create a subdirectory namedgcc-4.9.2. Next, we’ll configure our GCC build. As theGCC documentationrecommends, it’s best to configure and build GCC in another directoryoutsidegcc-4.9.2, so that’s what we’ll do.

mkdir build-gcccd 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 toconfigure:

  • The--program-suffix=-4.9.2option means that once our new GCC is installed, we’ll run it asg++-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-bootstrapoption 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-bootstrapis good enough for our purposes. This will save a significant amount of compile time.
  • The--disable-sharedoption 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/localin Cygwin’s virtual filesystem. This will make it easier to launch the new GCC since/usr/local/binis already listed in Cygwin’sPATHenvironment variable. However, if you’re using an existing Cygwin installation, it might prove difficult to uninstall GCC from/usr/locallater 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/directoryto the aboveconfigurecommand.

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 theGraphite 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-j4option 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 aXeon-basedmachine having 12 hardware threads, and got the following build times:

Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (5)

Be warned: I encountered asegmentation faultthe 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 installcd ..

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 doesnotinstall 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 thelist_modifications.pyscript fromthis postto determine which files are safe to delete from/usr/local.
  • You can simply uninstall Cygwin itself, by deleting theC:\cygwin64folder in Windows, along with its associated Start menu entry.

4. Test the New Compiler

All right, let’s compile some code that uses generic lambdas! Generic lambdas are part of the C++14 standard. They let you pass arguments to lambda functions asauto(or any templated type), like the one highlighted below. Create a file namedtest.cppwith the following contents:

#include <iostream>int main(){ auto lambda = [](auto x){ return x; }; std::cout << lambda("Hello generic lambda!\n"); return 0;}

You can add files to your home directory in Cygwin using any Windows-based text editor; just save them to the folderC:\cygwin64\home\Jeff(or similar) in Windows.

First, let’s see what happens when we try to compile it using the system GCC compiler provided by Cygwin:

g++ --versiong++ -std=c++1y test.cpp

If the system compiler version is less than 4.9, compilation will fail:

Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (6)

Now, let’s try it again using our freshly built GCC compiler. The new compiler is already configured to locate its include files in/usr/local/include/c++/4.9.2and its static libraries in/usr/local/lib. All we need to do is run it:

g++-4.9.2 -std=c++14 test.cpp./a.exe
Installing g++ (C++ Compiler) on Windows - Amir Masoud Sefidian - Sefidian Academy (7)

It works!

https://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/

Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated: 01/01/2023

Views: 6083

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.