Compiling process in C
Compilation is the process of converting code written in a programming language to machine code. During this process, the code goes through different steps depending upon the compiler and language. We will explore the compilation steps in C with the gcc compiler.
1: Preprocessor.
The source code goes through the preprocessor, which removes the comments and replaces the include header files with the actual content of the header file.
The command to see the preprocessed code is
gcc -E code.c
2: Compiler.
The code, now expanded by the preprocessor, is passed to the compiler. The compiler converts this code into assembly code.
3: Assembler.
The compiler will convert the preprocessed code into assembly code. The assembly code is the code written in assembly language.
The command to generate the assembly code is:
gcc -S code.c
4: Linking.
In this step, our assembly code is linked with the other code we are using. For example, if we are using printf function our object code will be linked with the object code for printf or rather the library code containing printf and an executable will be created.
To create the final executable use
gcc code.c
In summary:
gcc -E code.c # generates preprocessed code
gcc -S code.c # generates assembly code
gcc -c code.c # generates object code
gcc code.c # generates linked object code