The Process of Compiling a C Program
In this article, I will talk about the process of compiling a C program. Before we get started, I will brief what C language is and what gcc is, then it will help to understand the compiling process better.
What is C?
C is a programming language which appeared in 1972 firstly and its inventor is Dennis Ritchie. C is also a compiled language, unlike interpreted language, this means C’s source files will be complied to be executable.
gcc
While talking about the compilation of C program, we definitely need to mention gcc, which is the command to invoke the GNU C Compiler to compile the C code and produce object code. In other words, it takes the source code, decomposes it, generalizes the structure, restructures it, optimizes it and then converts it into machine code.
To get the compiling process started, you need to run below command –
$ gcc source.c
After we run gcc command, there are 4 steps during the whole compiling process, see below image -
Step 1 – Preprocessing
There are mainly three things happened in this step –
· Remove all the comments, as comments help people to understand the code, but machine doesn’t need them.
· Expand the macros. For example, in the source file, we define some macros, the preprocessor will replace the symbolic constants with their values.
· Expand the included files. If there is a header file such as # include <studio.h>, it will copy the header file into the source code file.
Recommended by LinkedIn
The output of this step is an expanded source code with the extension “.i”. For example, the file name will be “source.i”.
We can use “gcc -E source.c” to stop the compilation, so we have a look what the “source.i” file look like.
Step 2 – Compiling
The compiler will convert the “source.i” to “source.s” file which is in assembly level instructions. By saying that, I mean this file gets translated into intermediate code which can be understood by CPU. Also, it will check this file for syntax errors and optionally optimize the code for better performance. We can use “gcc -S source.c” to have a look the “source.s” file.
Step3 – Assembly
The assembler takes the intermediate code and convert it into object code, which is in machine language, for example, binary. This is totally unreadable for human beings. The output of this step will be stored in the “source.o” file. Use “gcc -c source.c” to get the “source.o” file.
Step 4 – Linking
The final step of compiling is to link all the objects files and libraries together and combine them and then produce a single file, normally this file will be able to execute.
· Link all the source files such as all other object codes in this project, or we can say that other object code will be linked to this “source.o” file.
· Link the calls of the function with its definitions so that the linker will understand where to look up for the function definition in the libraries, regardless it is static or dynamic.
In Windows, this file extension will be “.exe”, but in Linux, this file will be renamed “a.out”.