What happens behind when you compile a C file?

Diego Jeanluck Linares Castillo
3 min readFeb 10, 2022
GCC logo (GNU Compiler Collection)
GCC logo

When we code in C we are looking for efficiency and good control of computer resources but we need to compile the code to use it and for that, we have to use GCC.

But what is GCC?

GCC is a collection of compilers produced by the GNU project and supports programming languages like C, C++, Go, Objective-C, Objective-C++, Fortran, Ada, D, etc.

Having this in mind, we can now talk about the compilation of our C files as well as the step-by-step process.

First things first, install GCC

You have to run the next command on our terminal:

sudo apt install gcc

To check that you install GCC correctly:

gcc --version

You should have an output like this

gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Now you have GCC we can continue.

How to Compile a C File?

For a basic compile of a C file we have to write the next command on our terminal.

gcc <filename(s)>

After doing this, we get the a.out file, this is an executable file and inside it, we will find machine code.

And that’s it, you were able to compile your first c.

That’s all right?

Not really, there is more information on how to compile our files as an extended set of options that we will use depending on the situation, the best place to see these options is by using the following command in the terminal.

man gcc

But also here an example of the syntax

gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-Wpedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [@file] infile...

Well We’re ready to talks about the behind process of compilation

GCC Process

The compilation process consists of 4 stages to get the long-awaited program in machine code and they are the following.

1. Preprocessor:

This stage takes the C files and is responsible for removing code comments, extends included libraries (i.e. replaces include with library code), replaces macro names with code. Returns the extended code.

To see the extended code we can use the next flag when are we going to compile the C file(s).

gcc -E <filename>

When we use this flag the compiler return the result in the standard output. You can redirect the result to a file with the next command.

gcc -E <filename> > <new_file | another_file_name>

2. Compiler

This stage takes the extended code and transforms it into assembly code.

To see the Assembly code we can use the next flag when are we going to compile the C file(s).

gcc -S <filename>

When we use this flag the output file will have the extension “.s”.

3. Assembler:

This stage turns the Assembly code and transforms it into machine code. Returns the object code.

To see the object code we can use the next flag when are we going to compile the C file(s).

gcc -c <filename>

When we use this flag the output file will have the extension “.o”.

4. Linker:

This is the final stage, which is responsible for linking the object code files and merging them to get the final result of the compiled file.

If the C file contains functions from the library, the Linker will extract and connect them to the output file.

You can see the output using this command after compilation:

cat <compiled_file_name>.out

When we use this flag the output file will have the extension “.out”.

Note: You don’t need any flag to obtain the final file.

Thanks for reading.

--

--

Diego Jeanluck Linares Castillo

Autodidact | Passionate about technology | Holberton School Student