In this article, I will show you how to install GCC and compile C programs in Linux using GCC. I will use Debian 9 Stretch for the demonstration. But I will show you how to install GCC on wide variety of Linux distributions. Let’s get started.
Installing GCC on Ubuntu and Debian GNU/Linux:
On Ubuntu and Debian GNU/Linux distributions, GCC is really easy to install as all the required packages are available in the official package repository of Ubuntu and Debian. There is a meta package called build-essential, which installs everything you need in order to compile C and C++ programs on Ubuntu and Debian GNU/Linux distribution.
First, update the APT package repository cache with the following command:
The APT package repository cache should be updated.
Now install build-essential with the following command:
Now press y and then press <Enter> to continue.
GCC should be installed.
Now you can check whether GCC is working with the following command:
Installing GCC on Linux Mint:
You can install GCC on Linux Mint the same way as in Ubuntu/Debian as shown in the earlier section of this article.
Installing GCC on CentOS 7 and Fedora:
On CentOS 7 and Fedora, GCC is easier to install as well. The required packages are available in the official package repository of CentOS 7 and Fedora. You can install the Development Tools group to install all the required packages to compile C and C++ programs on CentOS 7 and Fedora.
First, update the YUM database with the following command:
YUM database should be updated.
Now install Development Tools group packages with the following command:
Now press y and then press <Enter> to continue.
If you see this message, just press y and then press <Enter>.
GCC should be installed.
Now you can check whether GCC is working with the following command:
Installing GCC on Arch Linux:
You can install GCC on Arch Linux too. All the required packages are available in the Arch package repository. Arch also has a meta package base-devel, which you can install to get all the required tools needed to compile C and C++ programs on Arch Linux.
First, update the Pacman database with the following command:
Pacman database should be updated. In my case, it was already up to date.
Now install base-devel package with the following command:
Now press <Enter> to select all unless you want to install very specific set of packages.
You may see something like this. It’s nothing serious as far as I know. It’s just a package was renamed from pkg-config to pkgconf. So Pacman is asking you whether you want to use the new package and remove the old one. Just press y and then press <Enter>.
Now press y and then press <Enter>.
GCC should be installed.
Now check whether GCC is working with the following command:
Writing Your First C Program:
Now let’s write a very simple C program, which we will compile in the next section of this article below using GCC C compiler.
First, create a project directory (I am going to call it hello) with the following command:
Now navigate to the newly created directory with the following command:
Now create a new C source file (I am going to call it main.c) here with the following command:
Now open the file with any text editor (such as vim, nano, gedit, kate etc) of your choice.
To open the file with nano, run the following command:
To open the file with vim, run the following command:
To open the file with Gedit, run the following command:
To open the file with Kate, run the following command:
I am going to use Gedit text editor in this article.
Now type in the following lines and save the file.
Here, line 1 includes the stdio.h header file. It has function definition for the printf() function I used on line 4.
Every C program must have a main() function. It is the function that will get called when you run a C program. If you don’t write a main() function, you can’t run the C program. So I wrote a main() function in line 3 – line 7.
Inside the main() function, I called printf() library function in line 4 to print some text to the screen.
Finally, in line 6, I returned 0 from the program. On Linux world, when a program returns 0, it means the program ran successfully. You can return any integer you like but there are some Linux specific rules on what return value means what.
In the next section, I will show you how to compile the C program with GCC and run it.
Compiling and Running C Programs with GCC:
The command to compile a C source file with GCC is:
NOTE: Here, SOURCE_FILES is a whitespace separated list of C source files. The compiled executable file will be saved as OUTPUT_BINARY in your current working directory.
In our case, the main.c source file doesn’t depend on other C source file, so we can compile it with the following command:
The source file main.c should be compiled and hello executable file should be created as you can see in the screenshot below.
Now, you can run the hello executable binary file as follows:
As you can see, the correct output is printed on the screen.
So that’s basically how you use GCC to compile C programs on Linux. Thanks for reading this article.