01101000 01100101 01101100 01101100 01101111 00100001 → Hello.
Many of us have heard or read that a computer only understands binary, if it is true, then how is that when we write a code like this, which is non binary:
The computer shows us something like this:
Well, that's thanks to the magic of compilation, think of it as “translating” to another language, that way, both, we and the computer can communicate and understand each other. When we execute the compilation process, we are translating the code we just wrote to the only language the computer understands: binary, it is ones and zeros, nothing else.
There are four main steps when compilating: preprocessing, compiling, assembly, linking.
Let`s compile the main.c file to discover what is happening on each of those steps, it is done by typing the command gcc main.c on the shell prompt.
#include <stdio.h>int main(void)
The source code file is given an extension “.c”.
Preprocessing is the first step, in this stage:
- Comments are removed.
- All included files and macros are expanded.
- look for what is included in the header, <stdio.h> for example and copy the header file into the source code.
- A “.i” file is created with all this info.
Then comes Compiling, in this point the process takes what was generated on the preprocessing and translates it one step closes to the computer language: assembly language, this is a middle ground between human readable language and the machine language and takes it to the assembler.
A “.s” file is created
The machine language file is finally generated by the Assembler in the Assembly step (.obj extension file) and all the program is translated into binary code, also known as “object code” and is completely understandable by the machine.
And lastly, Linking, This is when all the object code form all the needed modules, is merged together into a single one and linked to the libraries from which we used functions by copying them if we are using static linking (if dynamic linking is use, the libraries are just named in the binary file)
A “a.out” excecutalbe file is created. we can specify a different output file name by typing -o option when compiling the program (gcc $current_file_name -o new_file_name) the program can now be run by typing ./a.out.
Keep in mind that every time we make a change to the code, we need to compile in order to execute the file.
There may be variations to this process like dynamically compiled languages and statically compiled languages but the main process is quite similar.