Skip to content

How to install a C++ compiler on Windows

June 20, 2011

One thing that often seems to bemuse people coming new to C++ programming on Windows is how to install a C++ compiler. I’m not talking here about a full-blown C++ IDE, just a compiler that lets you write something like this in notepad:

#include <iostream>
using namespace std;
int main() {
    cout << “hello world!” << endl;
}

and compile it from a command prompt with:

g++ hello.cpp -o hello.exe

and run it:

hello

and have it print:

hello world!

As Brian Kernighan observes in the first chapter of K&R, if you can do this you have jumped all the difficult hurdles. So how to do it on Windows? For the purposes of this tutorial, I assume:

  • you know how to use a web browser
  • you can use a command-line of some sort
  • you actually can install things (I don’t address Windows security issues)

The compiler we are going to install is called GCC. This stands for “GNU Compiler Collection”. This compiler (or more accurately collection of compilers) is probably the most widely used in the world today. It is free, open source software, licensed under the GPL, and has very good (though not complete) compliance with C++ standards, both old and new.

As it is FOSS software, GCC has been modified, changed, re-implemented and generally messed with by many, many people. This means that there are lot of different ways of getting hold of it. I’ll shortly describe what in my opinion is the best Windows implementation, but first let me warn you of several horrible alternatives:

  • MinGW

    Strangely, the compiler I’m going to recommend is a development of the MinGW compiler, but do not under any circumstances be tempted to download the MinGW compiler and tool-set from mingw.org – the chances of it working are approximately zero.

  • Cygwin

    This is an environment that may (or may not) allow you to build and run POSIX compliant programs (i.e. Linux/Unix) programs on a Windows host. It includes a GCC implementation. It also includes an unbelievable amount of crap. Unless you want the POSIX portability thing, which you probably don’t, avoid.

  • DevC++

    I mention this unholy beast (actually an IDE) because it has a horrid popularity among people who do not know what they are doing. It is useless and wrong. If you have installed it, you should uninstall it before continuing with this tutorial.

Having got that unpleasantness out of the way, we will turn to the compiler you should install. This will be the latest build of the MinGW GCC compiler from Twilight Dragon Media. Despite the name, you are not entering into a realm of elves, dwarves and role-playing, but are going to get an outstandingly well-supported implementation of GCC.

Twilight Dragon Media has a very minimal web page at http://tdm-gcc.tdragon.net. If you are expecting an all-singing-all dancing (by elves) JS experience you will (thankfully) be disappointed. What you get is a functional page with a big green button on it saying Download TDM-GCC. Click on that. This will take you to the download page. At the top of this page, is the Bundle Installer, which is what you want. Below it, you have a choice of 32-bit (on the left) and 64-bit (on the right).

Important: The 32/64 bit thing has nothing to do with whether your box has a 32/64 bit processor or whether you have a 32/64 bit version of Windows. You do not want the 64 bit version – choose the one on the left. This will take you to Source Forge, which unfortunately is where TDM GCC is hosted, and will do whatever your browser does when you download something. The download is currently about 18Mb. You should of course scan the download for viruses.

We are about to install GCC. At this point I would strongly recommend that you create a Windows restore point. The chances of the installation going wrong are remote, and GCC cannot really mess up existing software, but still. I’m a big fan of the restore point technology – one of the best things in modern versions of Windows. But it is up to you. Now:

  • Start the installer

    Launch the TDM installer you downloaded, by double-clicking on it. It will present a dialog asking if you want to create a new or manage an existing installation. Pick the first option – Create New. There is a check box at the bottom of the dialog that indicates if you want to check for updates. I always leave it checked.

  • 32/64-bit?

    Now you will be asked if you want a 32 or a 64 bit installation. You want 32 bit, no matter what your actual platform.

  • Where to put it?

    Now you are asked for the name of the installation directory. The name is up to you, is not significant, and will be created by the installer. I’d strongly suggest that you choose a directory name with no spaces in either it, or in the path leading to it – in other word, do not install in the “Program Files” directory. Like many tools that are derived from the UNIX environment, GCC has issues about path names that contain spaces. My own approach is to create a directory called

    C:\prog

    and install all my development tools as subdirectories of that directory. So for Twilight Dragon GCC 4.4.2 (for example), I would specify the install directory:

    C:\prog\tdm442

    but you could just as well forget the versioning and use:

    C:\prog\gcc

  • Which mirror to use?

    Now you get to pick a download mirror for the updates. There probably won’t be any, and the Source Forge default will do as well as any.

  • Which components?

    Lastly, you get to pick the components you want installed. You want the lot, and (importantly) make sure that the “add to PATH” option is checked as well.

  • Ready to go!

    You are now ready to go – click in “Install” and wait for it to complete. This won’t take very long. When it is done, you are given the option of looking at the README – this is always worth a quick glance.

If all has gone well, you now have GCC installed. To check it is there, open a new command prompt (this is important, do not use an existing prompt, as the PATH will be wrong) and type in:

gcc

You should get an error message:

gcc.exe: no input files

which is what you would expect.

Let’s now take a look at what you installed. In Windows Explorer navigate to the directory you installed GCC in. There are a number of subdirectories, but the crucial one is bin. Take a look in there – you should see a number of executables and DLLs. The important ones are:

  • ar – the GCC archive tool, used to create static libraries
  • as – the GNU assembler
  • cpp – the C preprocessor
  • g++ – driver program for compiling and linking C++ code
  • gcc – driver program for compiling and linking C code
  • gdb – the GNU debugger
  • ld – the linker

And now the moment you have been waiting for! From your command shell, move to a suitable directory (one you can create files in) and start notepad:

notepad hello.cpp

Enter the following program:

#include <iostream>
using namespace std;
int main() {
     cout << “hello world!” << endl;
}

and save it. Then at the command prompt, enter:

g++ hello.cpp -o hello.exe

This tells GCC to compile hello.cpp as C++ code, and to produce the output executable hello.exe. If all goes well, g++ will produce no messages. When the prompt returns, type:

hello

to run your first GCC-compiled program.

5 Comments
  1. Danny permalink

    I am stuck at this point:
    “From your command shell, move to a suitable directory (one you can create files in) and start notepad”

    Not sure what you mean by moving the directory from command shell, I’m getting an
    error when I try to produce an executable
    “g++: hello.cpp : No such file or directory”

    thank you for your work!

    • @Danny:

      Open a command shell and then change directory using the chdir command to the place you want to create your program, which must be an existing directory (c:> is the command prompt here):

      c:> chdir somewhere

      then create the hello world program with notepad:

      c:> notepad hello.cpp

      enter the program text and then save the file from within notepad. Then compile:

      c:> g++ hello.cpp

  2. Sam permalink

    Thank you! I have been struggling with this all morning but have now managed to install a C ++ compiler with your instruction, hurrah!

  3. Danish permalink

    Can you please help in how to update the exisiting MinGW of code::blocks with this TDM MinGW32 rather then setting it up at a different place and then keeping the useless old one too?

    • > Can you please help in how to update the exisiting MinGW of code::blocks with this TDM MinGW32 rather then setting it up at a different place and then keeping the useless old one too?

      Well, the old one may not be useless. But if you want to replace it, simply delete the root MinGW directory and install the TDM version in the same place.

Leave a reply to Danny Cancel reply