Skip to main content

Compiling and Building C/C++ Programs in Windows with MinGW Compiler

The most popular open source C/C++ compilers for Windows are MinGW and Cygwin. MinGW is much less tedious to install hence I will start with this. Cygwin? Let's do that another day...

Process of Installing MinGW


1. Locate the MinGW installer (let's assume 64bit)
2. Download
3. Run installer (assume on C:/MinGW folder
4. Configure PATH
5. Test

How to Compiler and Build  a C/C++ Program

1. Write the source and save in desired folder (e.g. Hello.c)
2. Change directory to the one containing the source file
3. Run the Command Prompt, Power Shell or other shell such as Git BASH in this folder. [Shift+Right-Click will bring up a popup to allow the first two shells. Right-Click will allow you to run the Git BASH. See details below).
4. Issue the compiler command: gcc -o hello.exe hello.c  which means: Compile source file called Hello.c and name the output file Hello.exe
5. Run the output file as follows: ./Hello 

Locate MinGW Installer

 Google "mingw64" to locate the 64-bit version (32-bit also shows up). The location is:

https://sourceforge.net/projects/mingw-w64/

Select the items shown in the following eight images. The latest version as of this article is 7.2.0 (5th image). Select the latest edition. The 6th image shows the two versions - 32bit and 64bit (posix). If your computer is 64bit (most modern computers are 64bit), select the "threads-posix" version.









After downloading the installer, run it to install MinGW. For ease of management, it is recommended you install in an accessible folder (C:/MinGW64 or similar).


Configure PATH

After installation, locate and copy the path the bin file. Example:
C:\MinGW-w64\mingw64\bin

Your path may be different.

See the series of images below. To access system properties in Windows 10, press ALT+Pause/Break key combination on your physical  keyboard.


 


Click on Path in the System variables windows then select the New command below it to edit the Path variables (see 1 and 2 below).
 At this point, you can add a new variable by selecting "New" then pasting the path to the MinGW bin folder

The image below shows how to access the Windows PowerShell which has replaced the command prompt in Windows 10 [SHIFT+Right-Click]. The Git Bash shell can is installed together with Git here: https://www.atlassian.com/git/tutorials/install-git#windows

To access Git Bash in any folder, open the folder and right-click inside the folder. It brings up the popup menu with the "Git Bash Here" command.


To Test the C Compiler


 The C compiler is popularly known as "gcc" which originally stood for GNU C Compiler. It has since metamorphosed to a very versatile compiler that not only compiles C/C++ (and builds together with Make) but also Go, Objective C, Java, Fortran, ARM (see details at http://gcc.gnu.org/gcc-7/changes.html). As a result, GCC now refers to GNU Compiler Collection.

In Windows, this powerful set of tools that is the heart of Linux, can be accessed by installing MinGW or Cygwin. In this article, we use MinGW due to its lighter and simpler nature.

To test gcc, open any of the shells identified above (Command Prompt or PowerShell or Git Bash). Type the command: gcc --version
Thi should give you the gcc version number. Example output:


If yu do not get a response from gcc, it' troubleshooting time...

How to Compile and Build a C Program

1. Type the C code in a text editor such as Notepad++ and save the C file with a ".c" extension such as Hello.c taking care to save in a folder that  you have noted so you can find the file in the next step:



2. Open you shell in the folder in which you saved your C file and type the following command assuming your C file is named "Hello.c":
 gcc -o hello.exe newmain.c

This command will compile and build an executable called Hello.exe with Hello.c as the input file.

3. To run the executable, type: ./Hello
The "./" forces the shell to look in the current folder. It's possible to execute a file in another folder if you provide the correct path. E.g.: gcc -o c:/code/test3.exe c:/code/test3.c

Comments

Popular posts from this blog

How to Install Visual Basic 2017 Offline

This page offers a way to avoid the long and error-prone online installation of Visual Studio which is a massive package. Our interest is limited to Visual Basic and ASP.Net. One can, later, add other tools such as C# and C++. In this page, "VS" refers to Visual Studio. Installing Visual Basic 2017 Offline is not a simple process because Visual Basic 2017 is a part of Visual Studio 2017 that comes as one huge package (up to 35GB) in three versions: Community Edition (Free) Professional Edition (intended for a few people) Enterprise (intended for larger software teams)  Visual Studio by default uses a web-based install process that starts by downloading one of three the bootstrapper package from the VS download page . When you run the bootstraper, it will initiate download of the Visual Studio components that may be up to 35GB in size and takes a very long time to complete. The purpose of this page is to explain how to avoid the headache of this long and tedious

How to Install Packet Tracer 6.0.1 for Linux

This article is intended to help me remember the installation process for Cisco Packet Tracer 6 details of which seems to be hard to come by on the web. 1. Log in in to your Cisco Netspace account. Yes, you need to be logged in. 2. The " Download PT 6.0.1 " link on is on the NetSpace home page. 3. Click this " Cisco Packet Tracer " link shown below the folder icon as shown in the figure below: 4. Select  Cisco Packet Tracer 6.0.1 5. Select download location. 6. After completion of download, use your file manager to open that folder and rename the file to something such as "CPT6". It is a shell script so no file extension is required. 7. Change the file permissions to executable using command: sudo chmod 777 CPT6 from a terminal window opened in the download directory. This assumes your downloaded file is renamed CPT6 8. Launch the file with command: ./CPT6 from the terminal window. You will get the message: Self extracting archive... We

Android Applications with Intellij IDEA - 1

This article applies to Android applications in Windows using IntelliJ IDEA development tools. Windows 7 was used in this example but the explanation will work for other recent versions of Windows. It is quite a task learning how to use mobile application development tools. This page describes how to digitally sign your .apk file which is the  Android application package file. In short, your Android program file. Once you have created an APK file to launch it, simply copy to your device and use the Android file manager to install it. The picture below shows the digital signature keystore file, the unsigned APK file and the signed APK file. The procedure of signing the APK file is quite straighforward but first you must ensure your Java JDK is properly installed and that the Java tools are accessible on your commandline (Windows command console - what used to be called the DOS window). Most important is that your PATH environmental variable (In Windows 7 find this at: Control