What happens when you type gcc main.c

Idelahoz
3 min readJun 11, 2020

In order to understand what the gcc main.c command does we must first understand what gcc is and what it does

The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++, Objective-C, Fortran, Ada, Go, and D programming languages. Many open-source projects, including the GNU tools and the Linux kernel, are compiled with GCC.

Syntax

gcc [option | archive ]…

The options are preceded by a hyphen, as is usual on UNIX, but the options themselves can be multiple letters; multiple options cannot be grouped after the same script. Some options then require a file or directory name, others do not. Finally, various file names can be given to include in the compilation process.

What is “gcc main.c”

Here’s what happens when we GCC the file main.c

Three main steps happen when we compile code:
1. Reads the source file
2. Processes it
3. Links it with a runtime library

A lot happens when the code is being processed. Let’s unpack.

A compiler has multiple modules: preprocessor, compiler, assembler and linker.

When we write the file main.c, the preprocessor generates some intermediate file, that file is given to the compiler. The role of the compiler is that it compiles files generated by the preprocessor as input, and that generates assembly code, so it can convert our C program file into the assembly language. Computers can only generate binary code, which is why assembly language is the format it needs to be in.

Though, assembler code is still not understood by the machine — it needs to be converted into machine code. The converter that does this job: the assembler. The assembler module will convert the assembly code into the object code.

Lastly, the linker, the last module, links the object code (created by the assembler) with library functions code that we use (when we write our code). From that linkage, txt files are generated.

Let’s go in depth about each module.

The preprocessor does 3 tasks: removes comments from the code, includes the header file (standard in C files) into the generated file itself, and if any macros were used, will replace the macro name with code.

The compiler will take the file (created by the preprocessor) code and create the assembly code. The assembly code are comprised of mnemonics, instructions defined by english words.

The assembler converts the assemble code into the object code.

Lastly, the linker can play one of two roles:
1. Can merge multiple C files by compiling them, into one executable file.
2. Links our code (generated from the binary code of the assembler output) with the library function code.

There are two types of linking: static and dynamic. The linker decides what type of linking it will use.

The linker will pack all the code into a single file, which is famously known as the .exe file.

The linker will pack all the code into a single file, which is famously known as the .exe file.

--

--