Learn how to install 64bit GCC (MingW-W64) on Windows 10/11

RSDevX
4 min readMar 2, 2023

The tutorial teaches you how to install 64bit GCC (MingW-W64) on a windows 10,11 system from scratch.

Original tutorial

Learn how to install 64bit GCC (MingW-W64) on Windows 10/11 for Win64 dev

The tutorial is aimed at somebody starting on C/C++ development in a 64 bit environment using GCC for the first time

When you google GCC for Windows ,there are no official Windows versions from the GCC project.If you go to the MingW-W64 website you will find out a plethora of options. In this tutorial we will teach you to select the correct GCC version for your project

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.

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

--

--