What happens when you enter gcc main.c in your shell?

Gianluca Dorelo
2 min readSep 21, 2020

Ever programmed in C? Then you must know GCC, the magic three letters that transforms the C source code into a program that can be executed. But what exactly is going on when you send the source code (main.c in this case) through it?

GCC stands for GNU Compiler Collection. It was first developed by Richard Stallman, an evangelist for open source software and founder of the GNU project. Known as the GNU C Compiler at the time, GCC has evolved to serve other languages such as Java and C++, and has changed its name to GNU Compiler Collection to reflect the multitude of languages it now supports.

There are four steps in the GCC compilation process: preprocessor, compiler, assembler, and linker. Essentially, the compiler takes the source code, which is written in human-understood form, and breaks it down to binaries that computers can read in order to execute it. The process is not unlike our digestive system: a hamburger is chewed down into small pieces in our mouth, and got broken down into smaller molecules as it passes through our stomach and intestines (so that the body is able to absorb it), until it uh, got merged with all other food processed the same way and form a cohesive (ideally) output from our rear end.

First (Preprocessing): The first stage of compilation is called preprocessing. In this stage, lines that start with a pound(#) character are interpreted by the preprocessor as preprocessor commands, also all comments in the code are deleted. Hence this commands are replaced by the source code itself

Second step (Compilation): In this process, the output of the preprocessor is passed into the compiler and the compiler generates an assembly code. The computer will start to add different components onto the stack. Assembly language describes the individual instructions the central processor will have to follow when running the program.

Third step (Assembly): Since our computers cannot interpret assembly code, the assembler translates it into the object code (or machine language), which consists of pure binary code that the computer can process and execute.

The final step (Linking: putting it all together): The linker accepts the “main.o” file at the beginning as input and once it has all of the separate pieces of object code, it fits them together like a puzzle to form the executable program.

--

--