Compiling a C Program in Linux

Have you recently switched to Linux and stuck wondering how to compile your C program? Or, are you just getting into the C language and don’t know where to start? No need to worry, in the following post we will be taking a deep dive into compiling and running your program. Linux comes stocked with tools that can be used for furthering your development in C. Let’s take a closer look.

Writing your C program

Let’s begin by writing a simple C program. I will be using Vim to write the code (Vim is also written in C). The Vim editor comes with most Linux distributions, so it is widely available. Due to this, it’s good to get familiar with the editor. However, any text editor should work for writing C code.

#include <stdio.h>
int main() {
    int num1, num2, sum;
    
    num1 = 2;
    num2 = 6;
    sum = num1 + num2;

    printf("The sum of %d + %d = %d\n", num1, num2, sum);
    return 0;
}

As we can see, we initialize three integer variables so they can be stored in a memory location on our system. The first variable, num1, is used to hold our first number. In the code above, I assigned the variable num1 to the integer 2. The second variable, num2, is used to hold our second number. In the code above, the variable num2 is being assigned to the integer 6. Lastly, the third variable, sum, is used to hold the sum of these two integers.

After the assignment of these variables, we use the printf() function to print the result of the addition to the screen. Lastly, we return the integer 0 as it’s required by the main function and it lets the shell know the program’s execution was successful.

We have a program, now what?

After our C program is written, then we need a way to run it. Luckily for us, Linux comes with a nifty C compiler, GCC (GNU Compiler Collection). The GNU compiler was created by the GNU Project. The compiler allows us to compile our C or C++ code and turn it into an executable. In order to fully understand the GNU compiler, I think it is a good idea to see how compiling works.

Compiling Stages

The compiling process can be broken up into four separate stages – preprocessing, compilation, assembly, and linking.

The preprocessing stage involves looking for characters such as # and interprets them as commands. Before interpreting the commands, the preprocessor does other things such as removing comments created by the programmer, and joining continued lines.

The compilation stage takes our preprocessed code and translate it into assembly instructions. The assembly instructions will depend on the system’s processor, although nowadays the two most common architectures are x86 and ARM. You will be able to understand the assembly instructions if you’re familiar with the architecture and the Assembly language.

The assembly stage involves taking the assembly instructions and translating them into object code. Object code is understood and ran by the system’s processor. If you’re curious about the output of the object code, you can use the hexdump command to read its output. Using the command is necessary as the output is in a binary format.

Lastly, the linking stage will arrange parts of the object code so functions can call other functions. This also involves including instructions for library functions. The file produced is the executable, which allows us to run our C program.

Compiling with GCC

Now that we understand the basics of compiling and what’s involved in its stages, let’s compile our C program. To use the GNU compiler, we simply type the command gcc followed by the arguments we want to use. We won’t go into much detail of the GNU compiler and what arguments can be used, as it’s quite an extensive list. However, you can type the command man gcc for more information about the usage of the GNU compiler.

To compile our C program, type the following command:

gcc <filename>.c -o <filename>

The first argument tells the GNU compiler what file we’re trying to compile. The -o option is used to name the output file. In this case, I’m using the same name as the file that contains the C code. However, you can name the output file whatever you’d like.

Now that the program is an executable, we can run it by typing:

./<filename>

Here’s the output we receive:

The sum of 2 + 6 = 8

Well done! We have created and compiled our first C program! I hope you have found the article to be useful, and gained more insight as to what exactly happens when compiling a C program.