GCC stands for GNU Compiler Collection. From its name, you can easily perceive that it is basically a group of compilers for different programming languages such as C, C++, FORTRAN, Ada, etc. This is one of the most extensively used compilers because it supports multiple programming languages. In this article, we will try to learn the method of installing GCC on Ubuntu 20.04 LTS. Moreover, figure out the method of running a basic program with GCC.
Method of Installing GCC on Ubuntu 20.04:
For installing GCC on Ubuntu 20.04, the following series of steps should be performed in the specified order:
Step # 1: Updating the System:
The first thing you need to do is to launch the terminal by looking for it in the Activities menu. Once the terminal window is there in front of you, you need to update the cache to fix any potential broken packages or dependencies. This can be done by executing the command stated below in your Ubuntu 20.04 terminal:
sudo apt update
When system cache has been successfully updated by this command, your terminal will display the output shown in the image below:
Step # 2: Installing the build-essential package:
Now, the build-essential package should be installed in which GCC resides. You can have this package on your system by executing the command shown below:
sudo apt install build-essential
Once this package has been successfully installed along with all of its components, the following output will appear on your terminal:
Step # 3: Installing the Manual Pages:
Additionally, you can also install the manual pages for this package for assisting you whenever you get stuck while using it. You can install the manual pages with the help of the following terminal command:
sudo apt-get install manpages-dev
After executing this command, the manual pages for these packages will be installed on your system which can be seen from the image below:
Step # 4: Verifying the Installation:
Once the installation of GCC has been successfully carried out, you can confirm with the help of the following command:
Running this command will display the version of GCC installed on your Ubuntu 20.04 terminal:
Step # 5: Creating a Test Program:
After installing GCC on Ubuntu 20.04, you might want to see whether this compiler is working properly or not. To check the working of GCC, you need to create a test program. We will be writing a program in C language for demonstrating the usage of GCC. First, we will create a file with the .c extension via the terminal by running the following command in it:
sudo nano FileName.c
You can also use any other text editor of your choice if you do not want to use the nano editor. FileName is just a dummy name which is to be replaced by the name of the file that you have created. For our particular example, we have named it as TestProgram.c.
Running the above-mentioned command will create a new file with the .c extension and will open it in the nano editor. Once the newly created file is opened in front of you, type the code shown in the image below in your newly created file. Here, the first line of the code includes the stdio.h library to your program with the help of which you can easily run the input and output commands. Then in the main() function of the program, we have simply printed a message by making use of the printf command. The “\n” parameter in the message is used to introduce a new line delimiter. The “return 0” statement will simply return an integer value to the main function after the successful execution of this program. After typing the code shown in the image below in your file, press Ctrl+ X to save it and to exit from the nano editor.
Step # 6: Compiling the Test Program:
Now you need to compile this newly created program to create its executable file by running the following command in your terminal:
gcc TestProgram.c –o TestProgram
Here, the first TestProgram indicates the .c file that we have just created whereas the second TestProgram is the name that will be given to the executable file after running this command. You can also have any other name of your choice for this executable file.
Step # 7: Running the Test Program:
Once the executable file has been created, you can simply run it by executing the following command in your terminal:
Running this command will simply execute your TestProgram file and will display its output on the terminal as shown in the image below:
By going through the series of steps discussed in this article, you can conveniently install GCC on Ubuntu 20.04 within a matter of a few minutes. Moreover, this tutorial also explains to you the method of running a basic C program by making use of this compiler.