In this tutorial ,We will learn How to install 64 bit (GCC) GNU Compiler Collection (Mingw-w64 port) on a Windows 10 system using MSYS2 installer for C/C++ software development.
Contents
- GCC on Windows Platform
- What is Mingw-w64
- What is MSYS2
- Compiling C/C++ file on Windows-10 using GCC (MingW-w64/MSYS2)
- IDE's for C/C++ development on Windows
GCC on Windows Platform
GNU Compiler Collection (GCC) is an compiler produced by the GNU Project supporting various programming languages like for Objective-C, Objective-C++, Fortran, Ada, D and Go. Here we will be concentrating only C/C++ development on Windows 10.
GCC along with other utilities like GNU Binutils for Windows (assembler, linker, archive manager), set of freely distributable Windows specific header files and static import libraries have been ported to Windows platform and is available in the following flavors.
- MinGW - ("Minimalist GNU for Windows") which can be used for creating 32 bit native executable on Windows platform
- Mingw-w64 - 64bit version
Please note that we will not be covering WSL or Windows Subsystem for Linux.
What is Mingw-w64
Mingw-w64 is a fork off MinGW which provides a more complete Win32 API implementation including Win64 support,C99 support, DDK, DirectX.Mingw-w64 can generate 32 bit and 64-bit executables for x86 under the target names i686-w64-mingw32 and x86_64-w64-mingw32.
Here we will be using Mingw-w64 for C/C++ native application development using Windows API as most modern PC are now 64 bit.
Mingw-w64 Project does not provide any binaries for download from its website instead it maintains a list of Pre-built toolchains and packages provided by other vendors.
These prebuilt toolchains contains GCC, Debugger ,Package Manager ,Terminals and a set of other Unix tools like curl, openssl, sed, awk etc which can be used for development on Windows Platform.
Here we will be using a Mingw-w64 package called MSYS2 Software Distribution
What is MSYS2
MSYS2 is a collection of tools and libraries providing the developer with an easy-to-use environment for building, installing and running native Windows software.
MSYS2 Software Distribution consists of
- command line terminal called mintty,
- Bash,
- tools like tar and awk
- build systems like autotools,
all based on a modified version of Cygwin. Despite some of these central parts being based on Cygwin, the main focus of MSYS2 is to provide a build environment for native Windows software and the Cygwin-using parts are kept at a minimum.
To install and remove various software packages internally MSYS2 uses Pacman as their package manager.
MSYS2 is sponsored by Microsoft Open Source Programs Office through their FOSS Fund.
Installing MSYS2 on Windows
Installing MSYS2 on Windows 10 is quite easy. Download the executable using the above link and run it.
After the binary is installed on your system ,
MSYS2 comes with different environments/subsystems and the first thing you have to decide is which one to use.
The differences among the environments are mainly environment variables, default compilers/linkers, architecture, system libraries used etc.
If you are unsure, go with UCRT64.
UCRT (Universal C Runtime) is a newer version which is also used by Microsoft Visual Studio by default.
It should work and behave as if the code was compiled with MSVC.
You can start a terminal .The default terminal is Mintty (above)
Install GCC on Windows using Pacman on Windows 10
MSYS2 on Windows 10 uses pacman as its package manager.After installing MSYS2 ,you can check the installed packages by typing
$pacman -Q
on the Mintty terminal. This will list all the available packages on your system as shown below.
GCC will not be installed by default, So you can go to the package repo and search for gcc.
You can now use pacman to install gcc on Windows.
$pacman -S gcc
Installation process of GCC using pacman on Windows 10 (below)
After which you can check GCC by issuing the whereis command.
$whereis gcc
Compiling C/C++ file on Windows-10 using GCC (MingW-w64/MSYS2)
Once we have installed the Compiler, we will compile a hello world C and C++ code using the installed GCC on Windows 10.
Compiling a C file using GCC (MingW-w64/MSYS2)
Type the below simple code on a text editor like notepad.exe or other text editors like Sublime Text,Notepad++
Save the below C Code as main_c.c
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Compile and run the code using the below commands
$ gcc -o main_c main_c.c
$ ./main_c
Compiling a C++ code using GCC (MingW-w64/MSYS2) using g++
Here we will be compiling a simple C++ file using the g++ compiler in the MSYS2 (MingW-w64) tool chain.
Save the C++ Code as main_cpp.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello from MSYS2\n!";
cout << "Compiled C++ File\n!";
}
Compile and run the C++ code using g++ compiler as shown below.
$ g++ -o main_cpp main_cpp.cpp
$ ./main_cpp
Compiling a Win32/64 API GUI C code using GCC (MingW-w64/MSYS2)
Now we will compile a Win32/64 GUI application using GCC (MingW-w64/MSYS2) on Windows 10 .
We will create a Window and a Message Box using Win32 native api and configure the gcc to compile and link to the native system API.
Copy the below code and save it as win.c
#include<Windows.h>
int WINAPI WinMain( HINSTANCE hInstance, //the instance of the program
HINSTANCE hPrevInstance, //the previous instance
LPSTR lpCmdLine, //ptr to command line args
int nCmdShow) //display properties
{
HWND h;
h = CreateWindow("BUTTON",
"Hit me",
WS_OVERLAPPEDWINDOW,
350,300,250,100,0,0,
hInstance,0); //Create a window with BUTTON class
ShowWindow(h,nCmdShow); //show the window
MessageBox(0,"Press Me","Waiting",MB_OK); //used to display window
}
You can compile the Win32 API code on GCC using the following command
$ gcc -o win win.c -Wl,--subsystem,windows -lgdi32
$ ./win
Here
The -Wl,--subsystem,windows linker switch ensures that the application is built as a Windows GUI application, and not a console application. Failing to do so would result in a console window being displayed whilst your application runs
You should link with the gdi32 library using "-lgdi32 " otherwise the code will complain about "undefined reference to GetStockObject".
Light weight IDE's for C/C++ development on Windows.
The above examples were all done on command line, Now if you want a full featured opensource C/C++ IDE for development on Windows ,you can check the below list.
Code::Blocks IDE
Code::Blocks is a free C/C++ and Fortran IDE that is designed to be very extensible and fully configurable. Built around a plugin framework, Code::Blocks can be extended with plugins.
Code blocks IDE works straight out of box and has a version that comes with GCC compiler bundled with it.
Documentation of the IDE is also quite Good.
It is a light weight IDE that taxes your system very little.
Code Lite IDE
CodeLite is an open source, free, cross platform IDE, specialized in C, C++, Rust, Python, PHP and JavaScript (mainly for backend developers using Node.js) programming languages which runs best on all major Platforms ( OSX, Windows and Linux ).
Documentation not that good.
Setting it up was not that easy
- Log in to post comments