What happens when you type gcc main.c?
GCC, short for GNU compiler collection, is a compilation program that takes your high-level (human language) code and translates it into low-level machine code (binary). It consists primarily of 4 individual modules:
- Preprocessor — removes any comments and translates macros (if any) into code
- Compiler — translates the code into assembly code
- Assembler — translates the assembly code into object code
- Linker — links our output with the necessary library
But what does all that really mean?
The preprocessor takes your files (in our diagram, both .c files) and creates a new file of extension ‘.i’. These files have undergone the process described above and are free from comments and macros have been translated. This preprocessor now feeds these new files as input for our compiler. The compiler then takes that input and translates it into assembly code. Assembly code is simply a mnemonic language which gives the computer a set of human-language instructions. Now with the easily digestible assembly code, our assembler is ready to interpret these newly generated .s files and translate them into object code (.o). Finally, this machine code generated by our assembler finds it’s way to the linker which essentially combines everything we need into one neat little .exe file.
Through this process we have walked through the steps a computer takes from starting with a plain text main.c file all the way through compilation until it arrives at it’s .exe destination.