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...
1. Locate the MinGW installer (let's assume 64bit)
2. Download
3. Run installer (assume on C:/MinGW folder
4. Configure PATH
5. Test
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
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).
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.
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...
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
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
Post a Comment