Coding · October 16, 2018 0

C program build process

C program build process is to convert the source code of the program to executable and this includes many steps along the way. Each individual step is responsible of a specific function and produces a new code output to be used by next step.

All these steps are as per the diagram below, for example we are using source code file proc_test.c:

Following are the description about each step and their output code details:

Preprocessing: In this step the source code is expanded for the preprocessor directives like #define #include #ifdef etc. In the source code where ever the preprocessors are used will be replaced by the actual definition of it hence expanding the code so as to remove the need of directives. The extension “I” of the output varies with environment and compiling of the program. The code is sent for compilation if this step generates no errors.

Compiling: Next step after expanding the code is compilation of the program to assembly code of the target architecture and operating system. Since we have cross-platform compilers available to us now it is possible to compile code to assembly of a different operating system. Also the syntax analysis of the code will be performed in this step and on successful compiling of the code the output assembly code will be stored in the file with extension ASM.

Assembling: Once code is converted to assembly code it is ready for conversion to relocatable object code. The relocatable object code is executable code except it is missing the memory address allocations in it. The code contains Text section(machine language code), Data Section(initialized global variables), Block started by Symbol(uninitialized global variable) and Symbol Table. Although the executable code is almost ready but it is missing the memory address information for the functions used in the code like printf() or functions defined in another file etc. The output file of this step is an OBJ file.

Linking: During the linking phase the uninitialized global variables are initialized and definition of external functions like printf() or the functions in another file are expanded in the code, making the code ready for execution. In addition to above mentioned all the data, text and other sections of different files are combined in one. Address allocation of the objects is taken care and the final output i.e. executable code is created. The most common error of linking process are misspelling of the functions which results in linker not able to find the functions address to link. In case of error in linking processes no executable is created.

Once the program passes through all the steps mentioned above executable file as final output is created which is ready to run.

Happy coding.

In next post we will go through step by step process of execution of the program  in operating system with the help of a debugger.

NOTE: this is incremental blog and i will keep adding new content to it whenever possible. Keep visiting this post for latest updates.