Firstly, let’s talk a little bit about C. This is a multipurpose programming language developed by Dennis Ritchie at the Bell Laboratories near the 70s, designed to create small and fast programs. This means that it is a language of a lower level compared to others, capable of creating code closer to what the machines understand.
The code that the machines understand is a binary flow from 1 to 0, so it is necessary to use a compiler that converts the C code into machine code. To compile a C program it is necessary to use the GCC compiler (collection of GNU compilers) which is considered the standard for open-source operating systems such as Linux.
Normally GCC does preprocess, compilation, assembly and linking so you can make use of the overall options that allow you to stop the process at an intermediate stage.
This is an example of how C and the GCC compiler work: we must create a source file that contains C code, which in this case will be the 5-print.c file.
Then you run the source code through a compiler that checks the errors, and if it is ok, compiles the source code:
The compiler creates a new file called an executable (printf), that contains the machine code as you can see in the next image, so this is the program we run.
The compilation process is divided into 4 phases:
Preprocessor: the first step, that removes the comments, macros are expanded, and header files are replaced with the text contained inside them.
Compiler: Take the file code “C” and create the assembly code.
Assemble: Converts the assemble code into the object code.
Linker: Combines the libraries and the code together to form into a single file an executable machine code (executable program).
Overall options (a few):
- -c: Compile or assemble the source files, but do not link. The linking stage simply is not done. Example file: main.o
- -S: Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. Example file: main.s
- -E: Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don’t require preprocessing are ignored.
- -o : Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
- -v: Print (on standard error output) the commands executed to run the stages of compilation.