What happens when you type gcc main.c ?

In order to understand what gcc does we need to know what a compiler is and how it works.

What is a com·pil·er?

As you can see in the picture above when you first write a source code it first has to compile the code. A compiler converts a program from a human-readable format into a machine-readable format.

There are many steps before the machine can actual read the code that is being entered. It first goes through these steps below:

1. Preprocessing —

Lines starting with a # character are interpreted by the preprocessor as preprocessor commands.This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the command internally.

2. Compilation —

During compilation the code is translated into assembly instructions specific to the type of processor architecture. The compiler (ccl) translates main.c into main.s. File helloworld.s contains assembly code. You can explicitly tell gcc to translate helloworld.i to helloworld.s by executing the following command.

[root@host ~]# gcc -S helloworld.i

After compiling it from source to assembly language, it coverts it to binary language. But what is assembly language?

3. Assembly—

An assembly language implements a symbolic representation of the machine code needed to program a given CPU architecture. Below is an example of what assembly code looks like and what it is doing.

This code is then translated by an assembler into machine language instructions that can be loaded into memory and executed. Now onto what machine language is. Machine language is binary which is language made up of 1’s and 0’s.

Now that it’s finally been translated and gone through all these steps its ready for the final step.

4. Linking—

During linking the pieces of object code is arranged so that the function in other files can call functions in some other files. It also adds back the part containing the library functions used by the program. The end result being that now you have an executable program.

Like what you read? Give Wendy Mayorga Segura a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.