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 RIOT Plugin for GIMP

How to Install RIOT Plugin for GIMP::  Since this article was first written a stand-alone version of RIOT has been made available Download the plugin from   http://luci.criosweb.ro/riot/download/   Select the download link titled " RIOT as plug-in for other applications "    Launch the  Riot-plugin.exe  file by double clicking. The installer installs all the files including Riot.exe, Riot.dll, and FreeImage.dll in the location where you installed GIMP  If GIMP is open, close it and re-launch it. To optimise an image, open it then save with menu  File->Save for web with RIOT    You can set the target file size by selecting " Compress to size " button  on bottom right corner of optimised image pane and providing the size in kilobytes. Alternatively you can use a slider provided to allow you to change the quality while showing you a preview of the resulting image. Enjoy .       ...

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

XUBUNTU PANEL AND WINDOW BUTTONS HAVE DISAPEARED

 XUBUNTU PANELS Xubuntu can be quite troublesome when you shutdown and restart. Desktop panels may disappear as well as window minimize/maximise/close buttons.  The panel is equivalent to the Windows 10 Start menu that provides access to applications. TO MAKE THE PANEL REAPPEAR Open a terminal by right-clicking on the desktop then enter the following: xfwm4 --replace TO RECOVER THE PANEL Open a terminal by right-clicking on the desktop then enter the following: xfCE-panel --restart  That is all . As we say in Kiswahili: Mambo kwisha!