Solarian Programmer

My programming ramblings

Install Code::Blocks and GCC 7 on Windows

Posted on November 22, 2017 by Sol

In this article, I will show you how to install a minimal C, C++ compiler toolchain and an IDE on Windows. More to the point, you will install GCC 7 and Code::Blocks. The advantage of this setup is that you will be able to compile any standard C99, C11, C++11, C++14 and C++17 program on your Windows machine. Please note, that Code::Blocks is available in two versions: as a standalone IDE, as an IDE and an outdated version of GCC (4.9.2). I will show you how to use the latest version of GCC with the Code::Blocks IDE.

Installing the nuwen MinGW distribution:

A simple approach to get GCC 7 for Windows is to use the nuwen Mingw distribution. This is a 64 bits distribution easy to install even for novices. You will get the full GCC 7 toolchain plus a few extra useful libraries (you can read more on the distro webpage if you are interested). Download the mingw-xx.y.exe self extracting archive from the above page and start the installation by double clicking on it. Be sure that the destination directory for the extracted archive is the root of one of your drives, e.g. C:\ or D:\:

Nuwen MinGW destination directory

Once the installer is done, you should have the distro installed on something like C:\MinGW or D:\MinGW. From now on, I will assume that you have everything installed in C:\MinGW. You can check if everything was correctly installed by running the open_distro_window.bat script from C:\MinGW. If you double click the above script a Command Prompt will open with the path correctly setup. Check the version of GCC installed with:

1 gcc --version

This is what I see on my machine:

Check GCC version

At this point, you should be able to use gcc, g++, make as usual. For example, if you have a C++ program, test.cpp, saved in C:\MinGW, you can compile and run it with:

1 g++ -std=c++17 -Wall -pedantic test.cpp -o test
2 test

Just remember, every time you need to use the compiler from the command line, you will need to start a Command Prompt window by double clicking open_distro_window.bat from C:\MinGW.

As a side note, the distribution also contains the Vim text editor. If you are familiar with Vim you can use it, from the above command prompt with:

1 git\usr\bin\vim test.cpp

That being said, you can use any good text editor like for example Notepad++, Visual Studio Code or Atom.

Now, that we have the latest GCC installed we can install a C++ IDE like Code::Blocks.

Installing the Code::Blocks IDE:

From the Downloads section of Code::Blocks select Download the binary release. On this page, select the version of the installer that doesn’t contain mingw in his name, typically this is the first option:

Code::Blocks installer

Start the installation process and accept all defaults. At the end of the installation you will get an error about not finding a compiler, ignore the message and close this window.

Open Code::Blocks and go to SettingsCompiler:

Code::Blocks compiler settings

Select the Toolchain executables and change the names starting with mingw32- by removing the above prefix. You can see the default settings in the next image:

Code::Blocks default toolchain settings

Here are the file names after I’ve removed the mingw32- prefix:

Code::Blocks default toolchain settings

Press OK (lower right corner) to register the changes.

Let’s see how you can create a new C++ project. Go to FileNewProject, select Console application and press Go:

Code::Blocks select project type console

On the next page, read the description of the project type and press Next. Select C++ (this should be selected by default) and press Next again.

Now, give a descriptive title to the project and select where you want the project to be saved, I’ve used Hello for name and C:\DEV for the project path:

Code::Blocks set the project name and path

Press Next and, on the final page, accept the defaults and press Finish:

Code::Blocks final settings

Next, go to the left panel, press the plus button to unfold Sources and double click on main.cpp to make it visible in the editor area. You should see a default hello world source code. Use the small green triangle button to build and run the project:

Code::Blocks build and run a project

Please note that the left, slightly larger, green triangle button is used to Run an already compiled program. If you change something in your code, save it and use the Build and Run button to generate a new executable.

If you have no error, a console window will open and show you the result of running the program:

Code::Blocks executing hello world program

By default, GCC 7 compiles your C++ code using the C++14 standard, for C programs the default standard is C11. If you want to use the latest C++17 standard you will need to set a compiler flag. You can do this from SettingsCompiler and Compiler Flags. Right click on one of the Have g++ follow … lines and select New flag:

Code::Blocks define a new compiler flag for C++17

press OK and make sure the right rectangle is checked:

Code::Blocks enable a compiler flag for C++17

press OK to register the changes.

Now, when you press Build and Run the compiler will use the C++17 standard. This change will persist between projects, if you need to use an older standard remember to deselect the C++17 flag.

If you are interested to learn more about the new C++ syntax, I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

comments powered by Disqus