Become One with gcc: An Overview of the Steps for C Compilation

If you’re interested in starting C, and somehow you managed to stumble your way to this blog post, the first piece of advice I can bestow upon you when trying to understand the C compilation process is to first type man gcc into your shell.

You might be overwhelmed with dense text but it’s useful to get into the habit of doing this in concert with your search engine scavenger hunts as many of the results you will find will directly reference all the flags you need.

It also doesn’t hurt that gcc is the central command that drives the process and gets you started on your way to running your C program. To understand what’s actually happening when you run gcc and how to manipulate the process to suit your desired effect via flags, it’s essential to break down the process into 4 steps:

  1. Preprocessing

The first stage can be likened to housekeeping. Preprocessing takes care of objectives dealing with functionality. One of these is comment stripping and removing any continued lines denoted with a \.

A key part of this step is file expansion: the act of taking all macros and header files and merging them with the source file for the sake of streamlining the further steps.

The flag to stop compilation at this step and create a .i file: 
gcc -E my_c_program.c -o my_c_program.i

2. Compilation

A step self-referencing the overall process? Yeah, a bit confusing. However, the idea behind it is to translate what was done in the preprocessing stage into assembly language — it’s translation is in relation to the target processor.

The flag to stop compilation at this step and create a .s file:

gcc -S my_c_program.i -o my_c_program.s

3. Assembly

More translation. The .s file holding assembly language is then converted into a machine code .o file.

The flag to stop compilation at this step and create a .o file:

gcc as my_c_program.s -o my_c_program.o

4. Linking

Finally, it’s time to glue all of this translation together.

A great post by “Codingfreak” elegantly sums this up:

“The final stage of the compilation process is producing a single executable program file by linking set of object files. An object file and an executable file come in several formats such as ELF (Executable and Linking Format) and COFF (Common Object-File Format). For example, ELF is used on Linux systems, while COFF is used on Windows systems.”

This is where the program can be executed and, you, the creator can see the results.

The More You Know…

There are excellent sources for further understanding on what actually happens when you’re trying to run your C program. Try some of the gcc flags that were shared as well as in man gcc. Breaking down each part and wrapping your head around the purpose and function will truly help in the long run.

Like what you read? Give Joseph McDaniel a round of applause.

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