Enable C++17 in Code blocks MinGw GCC for all version (with pictures)

Enabling C++17 in Code::blocks will also enable the C++11 and C+14 features.In this post, we will see how to enable C++17 in Code::Blocks MInGw GCC.

Installing code::blocks

I believe you already have Code::Blocks installed in your computer. If you have not installed it, go to the link here Code::blocks and download the latest version (in the time of writing this post the latest version is 17.12). If you have the lower version we can still enable C++17 features but it is better that you have the latest version (but if you have the lower version and do not want any extra work go directly to the next section). There will be two links for the download:

i)One is without MinGw Compiler.The file name looks like this “codeblocks-16.01-setup.exe“.
 
ii)The other is with MinGW compiler.The file name looks like this “codeblocks-16.01mingw-setup.exe“.

You can download any of the files and install it.


Downloading higher version of MinGW

The lower version of MinGW/GCC does not support C++17 features. We will use the higher version of MinGW/GCC. You can download the higher version here https://nuwen.net/mingw.html. In my time of writing this post the latest MinGW version available is 8.1.0. This version support all the C++17 features. Install this latest version in the directory “c:/” by running the .exe file.

After installing the MinGW get the directory of the binary files, which is the directory “C:\MinGW\bin” and add it in the environment variables. Read the next section on how to add the directory to the “environment variables”.

Adding the directory “C:\MinGW\bin” in the environment variables

Go to “This PC” or “My Computer“, right click and click on the “properties“, you will see a new window.The screen shot is provided below.

now click on the “Advanced System Setting“. This will take you to a new Window screen which is shown below.

environment-variables

In this Window click on the “environment Variables” button.This will take you to new Window, the screenshot is provided below.

Double click on the “Path” under the ‘System Variable‘ section. Double clicking on the ‘Path‘ will open a new window. In this new window, click on the ‘New‘ button and add the directory “C:\MinGW\bin“. The screen shot is provided below.

adding the directory

After adding the directory click ‘Ok‘ and restart your computer. After restarting your computer go to next section.



Integrating the higher MinGW version with code::blocks

Open your code::blocks and go to “settings” (at the top) and click on “Compiler…“, a new Window will pop up. In this new window, go to “toolChain executables“. The screen shot is provided below.

toolchain executables

In this window, we will change some settings. Under the “Compiler’s installation directory“, click on the “” button and among the directory shown select the directory “C:/MinGW“. After this,

i)Go down below, under ‘Program files‘ and for ‘C compiler‘ click the button ‘‘ at the right side and go to the directory “C:/MinGW/bin” and select “gcc.exe“.

ii)Go down again, for the “C++ compiler” click the button ‘‘ and go to the directory “C:/MinGW/bin” and select “g++.exe“.

iii)Go down again and for ‘Linker for dynamics libs‘ click the button ‘‘ and go to the directory “C:/MinGW/bin” and select “g++.exe“.

iv)Go down again and for ‘Linker for static libs‘ click the button ‘‘ and go to the directory “C:/MinGW/bin” and select “ar.exe“.

v)Go down again and for ‘debugger’ you need not do anything, so skip this line.

vi)Go down again and for ‘Resource compiler‘ click the button ‘‘ and go to the directory “C:/MinGW/bin” and select “windres.exe“.

vii)Go down again and for ‘Make program‘ click the button ‘‘ and go to the directory “C:/MinGW/bin” and select “mingw32-make.exe“.

After this click ‘Ok‘.The screen shot below might help you.

Enable C++17 in Code blocks MinGw GCC

After you have completed the above steps, click ‘Ok‘ go again to the “Settings” and click on “compiler…” under ‘Compiler flags‘ check the box which says “Have g++ follow the coming C++1z(aka C++17) ISO C++ language standard [-std=C++1z]“. This will make the compiler follow the C++17 rules while compiling. The screenshot is given below.

Enable C++17 in Code blocks MinGw GCC


Enabling the C++17 debugger

Now to enable the Debugger go again to ‘settings‘ select ‘Debugger…‘ and select the “Default” in the left column and change the ‘executable path‘ to ‘C:\MinGW\bin\gdb.exe ‘ and click ‘Ok‘. C++17 is now enabled completely.

To verify that C++17 features is enable.Create a project.Copy the code below and paste it in your main.cpp.


int main()
{
 const int i=90;

if constexpr (i) //'if constexpr' is part of C++17 
{ cout<< "True!"; }
else 
{ cout<<"False" ; }

cin.get();
return 0;
} 

If everything goes well you get the output as,

True

This also means C++17 is enabled.




Leave a Reply

Your email address will not be published. Required fields are marked *