Image is taken from https://hownot2code.com/2016/11/29/c-programming-tips/

COMPILING C

Vanessa Mususué
2 min readFeb 8, 2021

--

When we write a code in a file with the extension “.c” this file can’t be executed by itself, for this reason, we need a compiler to do this task. On Linux, we use the command “gcc ”, which is a compiler to C, C++, Objective C and Fortran.

Now we talk about how gcc works and the stages that it has. In the image below we can see the stages:

Image is taken from https://www.javatpoint.com/compilation-process-in-c

In the preprocessor the source code is expanded and the commentaries are removed. If you want to execute the “gcc” until the preprocessor stage, you can use the next code:

gcc -E name_file.c

The option “E” stops the compiler after the preprocessing stage.

In the compiler stage, the input information is processing to generate an assembly code. The command gcc with the option “S” stops after the compilation stage and the output is a file with the extension “.s”, you can use the next code to do it:

gcc -S name_file.c

The assembler stage returns the information in an object file, namely, in code machine (ones and zeros). With the option “c” the command “gcc” compiles but don’t make the linker stage and the output is a file with the extension “.o”

gcc -c name_file.c

Finally we have the linker stage that is in charge of adding other objects files and libraries, and its output is an executable file of the code that we wrote. In order to generate an executable file the only thing that we have to write is:

gcc name_file.c -o executable_file

The option “o” allows to name the output file with the name we want.

--

--