GCC. What is it and how to use it to compile your code.
GCC stands for “GNU Compiler Collection”
But before moving on, we need to clarify what a compiler is.
A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or “code” that a computer’s processor uses.
Basically put, it’s a translator that turns programs in a source language into the same program in a target language.
GCC is a compiler and related auxiliary tools which are used to compile different programming languages into binary and related formats.
To run a file through it, you use the command “gcc” next to the file to compile. It should look like this:
gcc ‘filename.c’
This is how the GCC compiler works:
Preprocessor
It first goes through the Preprocessor, where the code its cleaned to do better the compiler work, like remove comments, include required libraries, give the macros their values, etc. You can stop the compiler in this step using the command:
gcc -E ‘filename.c’
This will output a file with the “.i” extension.
Compiler
Now that the file is cleaned and ready, it goes through the Compiler. In this step GCC takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor. You can also stop the compiler in this step using the command:
gcc -S ‘filename.c’
This will also output a file, but with the “.s” extension.
Assembler
With the file cleaned and in assembly language, now it’s ready to go through the 3rd step, the Assembler. This converts the assembly code into machine code. If you want to also stop the compiler here, you can use:
gcc -c ‘filename.c’
This outputs an “.o” file.
Linker
And last (but not least) we have the Linker. In this step it merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code. This creates an executable file. If you want to make an executable file with a given name, you can use
gcc ‘filename.c’ -o ‘given_name’.
And this is what GCC is and how it works.