Compilation in C
A compiler is a program that translates a code written in one programming language (C in this post) into machine language, which is made of ob. We will use gcc as the compiler.
Let's start with a C program that will print "Hello World" and I will name it "hello".
This is how the file will look like
hello.c
And this is how the C instructions will look like
#include <stdio.h>
/**
* main - Entry point
*
* Return: Always 0 (Success)
*/
int main (void)
{
printf("Hello World\h");
return (0);
}
To compile this program and translate it to machine code so it can be excecuted, I will use gcc
$ gcc hello.c
This will initiate the compilation process, which is divided in four steps:
- Preprocessing: Is the first stage of compiling and it removes the comments and include header files in sorce code.
- Compiling: Once the code is free of comments, the compiling process translate it into assembly language (ASM), a low level language closer than C to the Machine Languange.
- Assembly: It translate the code that at this point is in assembly language, into object code, that contains a sequence of machine-readable instructions.
- Linking: Is the creation of a single executable file from multiple object files created in the previous step. The result of this stage is the final executable program or library.