How to compile a C program
The word compile means to translate a programming code to code executable by the machine. To compile a source code and make it executable there are different ways depending on the compiler used. Among these compilers, one of the most famous is C, and used for the bases of many programming languages.
To understand how a C program is compiled, it is important to know the different stages that are needed for the program to be executed by a user and by the computer.
This scheme can help us to enumerate the usual programming tasks. The most common task is the generation of an executable program. As the name implies, it is a file that contains code directly executable by the processor.
Based on the schemes, let's see the necessary steps to compile our program.
The preprocessor accepts as input source code and is responsible for:
Delete comments.
Interpret and process preprocessing directives, always preceded by the # symbol.
After this stage it becomes a readable file to perform the second step. To understand it better in our files we just wrote a program in c and save it as main.c
With the gcc -E command option, we say that only preprocess do not compile or anything like that, so our program becomes those lines of code, after that we follow the compiler to analyze the syntax and semantics of the preprocessed source code and translate it, generating a file that contains the object code.
With the gcc -c command we are saying that you compile us with the option that the new file to be output has the same name of the file to compile and that it ends with the extension .o, this command creates a new file for the assembler than what it is in itself to talk about that cool language in computer language as we will see now.
The linker groups the object modules (which often have extensions such as .o or .obj), generated by the compiler, into an executable program that the operating system can load and execute. It is the last phase of the compilation process.
With the gcc -S command we are saying that the main.o file assembles that file to be binary code.
Finally it is the use of libraries. All these functions are incorporated through a wide set of libraries that are not part, properly speaking, of the programming language. However, as indicated above, some libraries are automatically linked when generating an executable program, which leads to the error of thinking that, for example, printf () is a function of the C language
Thanks you :)