Complete guide to use gcc using Bash on Windows 10

In this blog I will explain you how to use gcc for compiling and executing C programs using Bash on Windows 10.
You need to first install Bash on Ubuntu on Windows 10. If you had not installed it yet, you can follow different sources available on internet.

Installing gcc compiler


To install gcc compiler, open Bash on Ubuntu on Windows application and write the following commad:

apt-get install gcc



This will install gcc. You can see the version of gcc installed using command:

gcc --version

Writing a C or C++ Program 

Now we will make a simple C program and learn how to compile and execute it.

To make a C program, first move to the destination folder using cd command and type the following:

nano hello.cpp

This command will open an editor with file hello.cpp
This looks like this:


Then write the program.
I have written a simple Hello World program.

Then press Ctrl+X to exit the editor. You will be asked Save. Press Y to save and exit.

Now you have created hello.cpp file successfully in your destination folder.

To see if it is created type the following command:

ls

This will diplay all the files in the folder, with one being which you created recently.

Compiling C program

To compile your program write the following command:

gcc hello.cpp -o hello


where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program.


This command compiles your source program if no error exist. If error exists it tells you the error name.
This command also creates a new compiled file (Here in our case "Hello").

Executing C Program

We will now execute complied file "Hello"

To Execute write the following command:

./Hello

This command will execute complied file and produce an output.



Uninstalling gcc

If you want to remove gcc compiler you can use the following command:

apt-get purge gcc





Comment below if you have any doubts.
Complete guide to use gcc using Bash on Windows 10 Complete guide to use gcc using Bash on Windows 10 Reviewed by Chirag Jain on Saturday, August 27, 2016 Rating: 5

No comments :

Powered by Blogger.