Compilation Process of C Programs

In this blog post we will explain some fundamentals concepts about compilation in C and illustrate with an example how is the process of it.

What is compilation?

What is a compiler

A compiled language (such as C or C++) is different from an interpreted language (for example, a shell script) or a pseudo-interpreted (for example, python).

In C, compilation will transform the C code of a program into native code, that is, a series of binary instructions that can be directly understood by the processor.

What is GCC?

More information check man gcc.

The phases of the compilation in c

Link: https://www.javatpoint.com/compilation-process-in-c

First of all we will write the source code in C language, we will create the lex.c file with emacs: emacs lex.c

Then, inside we will put the following code

#include <stdio.h>/**
*main - Print "Hello Lex"
*
*Return: 0 successful exit
*/
int main(void)
{
printf("Hello Lex\n");
return (0);
}

1) Preprocessing

If you want to print the result of the preprocessing stage, type this: gcc -E lex.c.

To resume the result we will add the command tail:

2) Compiling

Type the following code: gcc -S lex.c , now to see that you created the file lex.s, you will put ls lex* in your console:

To check the content of lex.s, type the following command: less lex.s

3) Assembling

The name of the object file generated by the assembler is the same as the source file. The extension of the object file in UNIX, the extension is “.o”.

Type the following code: gcc -c lex.s , now to see that you created the file lex.o, you will put ls lex* in your console:

To check the content of lex.o, type the following command: less lex.o

4) Linking

At the end of it all, the links of object files will help create an executable file.

Type the following code: gcc -o lex lex.o , now to see that you created the executable file lex:

To execute the file lex type the following command: ./lex, and check the result.

ALL IN ONE STEP!

$ gcc -o lex lex.c

Bachelor Industrial Engineering