Using MinGW for C Programming in Windows

0
1853

Though Windows and Linux are often perceived as incompatible with each other, it is possible to run free and open source software on Windows. C is still a very popular programming language. This article discusses how MinGW, an open source software development environment, can aid C programming in Windows.

Figure 1: Output of pgm1.c on Code::Blocks using MinGW

Instead of thinking of open source alternatives to Microsoft Windows, it also makes sense to think about open source software for Microsoft Windows. According to Wikipedia, “In the area of desktop and laptop computers, the share of Microsoft Windows is generally above 70 per cent in most markets and at 78 per cent, globally.” The Windows operating system is used both for technical and non-technical purposes. This article will focus on programming with C in Windows. C programming is still very popular, ranking among the top ten in almost all the programming language rankings. Since both C and Windows are still very popular, it is absolutely essential to learn about open source tools that can be used for programming in C in Windows.

The C language and UNIX have a very close-knit relationship. Any article on UNIX will mention C programming in the first paragraph itself, and vice versa. Some of the header files in C are developed specifically for UNIX. For example, header files like unistd.h, signal.h, etc, are added as part of the POSIX standard for UNIX-specific functionalities. Please note that whatever applies to UNIX here is also applicable to Linux. So C programming being closely related to UNIX makes it slightly troublesome when used on non-UNIX operating systems. Since MacOS is based on BSD Linux, it is relatively easy to do C programming in it. But programming with C in Windows is more problematic.

Let us discuss the different ways to program with C in Windows. There are still people who use Turbo C++ or Borland C++ on older versions of Windows. The use of such outdated IDEs is not a good practice but since this is often restricted to academic institutions, it need not be taken seriously. Serious C programmers on Windows typically use Microsoft Visual C++, an IDE that’s often called MSVC. This is, of course, very powerful and has a good library associated with it. MSVC also allows the user to develop applications by using Windows system calls. But there is a major problem with the MSVC compiler as far as an open source enthusiast is concerned. Even though MSVC is available as freeware and trial ware, it is essentially a proprietary software, making it anathema to the open source community. Even if this aspect of MSVC is overlooked as being just a philosophical argument, there is an even bigger problem with MSVC and C programming. MSVC only supports an older version of C called ANSI C (also known as C89). The newer versions of C like C99 and C11 are not supported, nor are there any immediate plans to remedy this situation. The lack of support for C99 and C11 is especially crippling to students of C who want to work with the latest versions of the language.

MinGW
If MSVC has so many drawbacks as a C learning tool on Windows, what other alternatives do we have? There is Cygwin, a POSIX-compatible environment that runs on Windows. But in this article we will discuss MinGW for programming in C on Windows. MinGW (Minimalist GNU for Windows) is a free and open source software development environment to create Windows applications. It is licensed under the GNU General Public License. MinGW includes a simplified port of the GNU Compiler Collection (GCC), a compiler system for Linux. It also provides developer utilities for Windows like an assembler, linker, etc. Freely distributable Windows-specific header files and libraries that support the use of the Windows application programming interfaces (API) are also provided by MinGW.

Since both Cygwin and MinGW can be used to execute C programs on Windows, let’s look at the differences between the two and why we ought to choose MinGW over Cygwin. The motto of Cygwin, which is ‘Get that Linux feeling on Windows’, clearly tells us its intended purpose. Cygwin helps users to port Linux applications to Windows. It acts as an intermediate layer between Linux applications and the Windows operating system. So, it is theoretically possible to run any Linux application on Windows using Cygwin, including GCC (the compiler systems of Linux).

Figure 2: Output of pgm1.c on Cygwin using GCC

So if GCC can be installed and executed on Windows using Cygwin, why should we use MinGW? The first reason is that installing GCC over Cygwin is relatively difficult and prone to errors, whereas the installation of MinGW on Windows is very easy. The second reason not to install GCC on Cygwin is that you need to work with terminal commands to execute the program, whereas it is easy to install and use an IDE on top of MinGW, simplifying the overall program development. The third reason is that it would be an overkill. If your intention is to program in C on Windows, all you need is MinGW and some IDE. Cygwin is much more powerful and is intended for a lot of other purposes too, executing C programs on Windows being just one among them. On a personal note, I believe Linux should be experienced on a Linux operating system and not on Windows using Cygwin. The fourth reason for not using GCC and Cygwin to execute C programs is that by using them, you are developing applications for the Linux operating system on Windows, whereas by using MinGW, you are developing applications for the Windows operating system on Windows itself. The point is that if you are planning to develop applications for Linux, the best platform is Linux itself.

To illustrate this final difference further, let us execute the following two simple C programs in both MingW and Cygwin. To execute the C programs with MinGW, we use the popular IDE called Code::Blocks, about which more will be shared later in this article. As mentioned earlier, to execute the program in Cygwin, we use a version of GCC, modified for use on Cygwin. Both the C programs use the library function system() to use system calls and commands of Linux and Windows.

The first program named pgm1.c is given below. It tries to execute the commands cd and date available in both Linux and Windows. Please note that the behaviour of the commands cd and date is different in Linux and Windows. Readers will understand this clearly after executing the programs with MinGW and Cygwin.

#include<stdio.h>
#include<stdlib.h>
int main()
{
system("cd");
system("date");
return 0;
}

The second program named pgm2.c is also given below. This tries to execute the Linux command pwd to print the name of the current working directory and the command date available in both Linux and Windows.

#include<stdio.h>
#include<stdlib.h>
int main()
{
system("pwd");
system("date");
return 0;
}
Figure 3: Output of pgm2.c on Code::Blocks using MinGW
Figure 4: Output of pgm2.c on Cygwin using GCC

Figure 1 shows the output of the program pgm1.c when executed with Code::Blocks and MinGW. The output shows that both the commands were executed properly. Since cd is a valid Windows command, the name of the current working directory is printed. Please note that the full path is printed in this case. The date command in Windows is used to print the current date and then modify it. If you look closely at the output, you will see that the date command is treated as a Windows command by MinGW; so it prints the current date in the Windows format and then prompts for a new date. Figure 2 shows the output of the program pgm1.c when executed with GCC on Cygwin. Note that the executable of the C program is a file named a.exe and not the traditional Linux executable file a.out. The command cd without a path specified doesn’t do anything in Linux. Since Cygwin treats cd as a Linux command no action is taken in this case. The date command in Linux only prints the current date. On observation of the output we see that the date command is also treated as a Linux command and thus the current date is printed in Linux format.

Figure 3 shows the output of the program pgm2.c when executed with Code::Blocks and MinGW. Since pwd is not a valid Windows command, instead of printing the name of the current working directory, the following error message gets printed: ‘pwd is not recognised as an internal or external command, operable program or batch file. If you look closely at the output, you will see that the date command is again treated as a Windows command by MinGW, and thus prints the current date in the Windows format and then prompts for a new date.

Figure 4 shows the output of the program pgm2.c when executed with GCC on Cygwin. Since pwd is a valid Linux command, the name of the current working directory is printed in this case. Please note that the current working directory that is printed starts from the root directory of Cygwin and not the absolute path printed in Figure 1 by MinGW. On observing the output, we see that the date command is again treated as a Linux command and thus the current date is printed in Linux format. So from the examples it is clear that MinGW and Code::Blocks know that they are working on a Windows operating system, whereas GCC and Cygwin act like they are working on the Linux operating system.

Code::Blocks
Now that we have seen the benefits of using a MinGW compiler on Windows for programming in C, the next important question is, ‘How do we set up MinGW in Windows?’. The answer is simple. You can download and install MinGW bundled with the popular IDE Code::Blocks. A simple search on Google with the keywords Code::Blocks and MinGW will lead you to one of the many mirror sites that offer a download. The file size is typically less than 100MB, and by double clicking the executable file installation can be started in Windows. On prompting, accept MinGW as your default C compiler from the list of compilers provided by Code::Blocks. Code::Blocks is a free and open source software licensed under the GNU General Public License. It is a cross-platform IDE which can be installed on both Windows and Linux. Code::Blocks supports multiple compilers like GCC, Clang, Visual C++, etc.

Figure 5: Logo of Code::Blocks

Code::Blocks has a lot of built-in tools which make programming a lot easier. It has the support of the popular Linux debugger called GDB (GNU Debugger). It also has the support of Make, the Linux build automation tool. The code editor of Code::Blocks has features like syntax highlighting, code completion, etc. All these features and tools available with Code::Blocks make it a very powerful tool for C programmers. Thus the combination of MinGW and Code::Blocks is a potent weapon for practitioners of C programming in Windows. But let me add one final point to our discussion before we conclude. Even though we have learned to program with C efficiently on Windows, the best way to explore the full potential of the C programming language is by using it on the Linux operating system.

LEAVE A REPLY

Please enter your comment!
Please enter your name here