Learn to Install GCC (Mingw-w64) Compiler Tools on Windows 10 Using MSYS2

by xanthium-enterprises in Circuits > Software

2830 Views, 1 Favorites, 0 Comments

Learn to Install GCC (Mingw-w64) Compiler Tools on Windows 10 Using MSYS2

Install 64bit GCC (MingW-W64) on Windows 10/11 using MSYS2 installer and compile win32/64 apps 2022

We will learn 

How to install 64 bit (GCC) GNU Compiler Collection on a Windows 10 system using MSYS2 installer for C/C++ software development.

After installing GCC, we will compile a Win32/64 GUI application using GCC (MingW-w64/MSYS2) on Windows 10 .


Contents

What Is GCC

1_atDwptvBK2KLfcnT9M_JgA.png

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 a set of freely distributable Windows specific header files and static import libraries have been ported to Windows platform 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 of GCC


What Is Mingw-w64

mingw-w64-install-windows10-tutorial.jpg
mingw-w64-pre-built-toolchains-package-binary.jpg

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.


Here we will be using a Mingw-w64 package called MSYS2 Software Distribution 

MSYS2 Software Distribution

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. 


o 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.

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. 

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

$pacman -S gcc

After which you can check GCC by issuing the whereis command.

$whereis gcc


Compiling C/C++ File on Windows10 Using GCC (MingW-w64/MSYS2)

First we will compile a simple c file using gcc.

Code below ,Save 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)


Code below, Save 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 code using the below commands

$ 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


More details about compiling Win32 API can be found here