Compilation in C language: A Step by Step Short Guide Of What It?Does.
Xavier E. Figueroa Muniz
Software Engineer at Lockheed Martin | Bachelor's in Computer Sciences | Holberton School
You are starting off with learning how to use C language and its pretty great! I have gotten to use it some time before and got to use it again recently, but there is quite more to it. If you have a basic understanding of Programming Logistics it can be easy going, but coding aside: Do you really know what is happening? How about we start of step by step. What is compilation? What is a compiler? What does it do? Some of these questions we will be trying to cover here.
So if you were to google “What is compiler?” you would normally get a result similar to this:
“Compiler: a computer program that translates an entire set of instructions written in a higher-level symbolic language (such as C) into machine language before the instructions can be executed “ — Merriam-Webster. (n.d.). Compiler. In Merriam-Webster.com dictionary. Retrieved September 16, 2020, from https://www.merriam-webster.com/dictionary/compiler
Now lets try to see how this goes. We can think of a compiler as some sort of dictionary or translator itself since as you may see from that reference when we write a code we are using a high-level symbolic language. Why is that? well computers have their own language such as ones and zeroes so in order to communicate with them we need a translator such as we also do in real life. But simply giving a straight up sentence is not enough. That’s where coding jumps in. We use a programming language (like C), which is a high-level programming or symbolic language. Why symbolic? Because its basically a coding language that is closer to our own way of communicating so it is closer to human language per se. Now the compiler what is does is basically translate this into the machine language. Now an example of a compiler is gcc (which stands for GNU Compiler Collection).
gcc main.c
What does it actually do?! Well the compiling process does take a series of steps to translate the code to to machine language: Preprocessing, Compiling, Assembly, and Linking.
Preprocessing is the first step of the compilation process in which the code basically goes through a series of checks such as:
Removal of Comments, Expansion of Macros Expansion of the included files, and Conditional compilation.
Basically it starts prepping the code and any files needed such as the stdio.h (which is a header file, but lets leave it for another topic) and peeling anything that is not needed like comments. Leaving it ready for the next process.
gcc -E main.c /* preprocess only*/
Compiling is the following step which uses the resulting output of the preprocessor stage and starts prepping it for the assembly process. Basically it generates an *.s file (ex. code_file.s) with assembly level instructions.
gcc -S main.c /* compile only*/
Assembly would be the next step which transforms the assembly code produced previously into a machine code or binary so the computer can understand it. This part leaves a file with a *.o extension (ex. code_file.o).
gcc -c main.c /* compile and asemble buy not link*/
Linking is lastly the final step in the process which as the name suggests it links all the functions used with any necessary data such as libraries. It can be done by either including a copy of said library or just a reference or name of it. It also adds a few more details such as start and end code that helps the machine understand it more.
As you can see in the following images using as a example a code that we made called main.c we get to see how this whole compilation process happens almost instantly and we don’t get to see any of it if we don’t want to. With simply a few command lines you’re done! Now it’s interesting to note that when you compile a *.c file normally an a.out file will be generated unless you specify some different name. This a.out file is the one that you execute on your command line in order to obtain what the code does which in this instance prints out “Hello, World”.
For any additional info I highly recommend to use on your command line gcc - -help or RTFM or simply put type man gcc. I’m no super expert when it comes to computer science in comparison to other professionals so do research a bit more to learn of all the options available since this is simply a short summary of what it does. Wish you all the best with your coding and hope you enjoy this post!