Build process in c

C Program building process

We can also say build process in c programming as compilation of a c program. It is the process of converting .c source file to an executable file.

There are many steps involved in this conversion from human readable code to machine understandable code.

This topic explains the abstract process behind the execution of a program. There are many steps involved in converting the C program into an executable form. They are as follows,

Build process in c
C program Build Process


Preprocessor is a text processing tool. All preprocessing directories begin with # symbol.

During this stage, C source code is expanded based on the preprocessor directives such as #define,#include,…

The Preprocessor interprets the preprocessor directive. For instance if you add #include<stdio.h> then it replaces it with contents of “stdio.h” file.

The ‘#’ symbol is also known as the preprocessor symbol in C. The expanded source code is stored in an intermediate file with .i extension.

Before proceeding to compilation of a c program the preprocessing must be done.

Preprocessing in c program build process
Preprocessing in c program build process


During this stage, the syntax errors are detected by the compiler. The errors hence detected are displayed on the terminal if any in the expanded file(i.e. a file with .i extension). Then error-free code is translated into an equivalent assembly language program with .asm or .s file extension. This assembly file thus converted varies from processor to processor.

Here lexical analyzer makes tokens. This token hence created is passed to the Syntax analyzer. The Syntax analyzer checks for syntactical errors in tokens. Then Semantic analyzer checks for the meaning of the code. The intermediate code generates small chunks of code. The code optimizer optimizes the code i.e. it removes unwanted chunks of code if any.

There are two steps they are analysis and synthesis. In the analysis step source code is broke into parts and creates an intermediate representation, synthesis part builds the program from the intermediate representation.

compilation process of a c program
compilation process of a c program


The assembler then translates .ASM program into a relocatable object code with .o or .OBJ file extension. The program here is relocatable because the specific memory address has yet to be assigned to the code. All the addresses are relative offsets.

The .obj file thus created is a specially formatted binary file that consists of a header and several sections. The sections are as follows

1.Text section: This section contains machine language code equivalent to the expanded source code.

2.Data section: This section contains global variables and their initial values.

3.Block started by symbol(Bss): This section contains uninitialized global variables.

4.Symbol table: This section contains information about symbols found during assembling of the program.

As external functions such as printf() and scanf() is not present in .obj file, this code is not executable. The functions and global variables defined in one .obj file can be used by another .obj file.

assembling process in c program build process
assembling process in c program build process


This is the final stage in creating an executable program. Following things happen at the time of linking,

1.Find definition of all external functions( ex: printf()) those which are defined in other .obj files and libraries.

2.Find definition of all global variables those which are defined in other .obj files and libraries.

3.Combine Data sections of different .obj files into a single Data section.

4.Combine Code sections of different .obj files into a single Code section.

During linking if linker identifies any library name misspelled , it stops linking process and doesn’t creates an executable file.

linking process of c program build process
linking process of c program build process


Here the code is ready for execution which resides on the disk. This code will be brought from disk to RAM by an operating system component called Program loader. This .exe file can be placed anywhere in the memory since all the addresses are relative. Once the loading process completes, execution begins from the first instruction in the Code section of the file loaded in memory.

The .exe file is a formatted file and thus differ from one Operating System to other.

People also ask for

Is linker a part of compiler?

A compiler generates object files from source code. A linker combines these object code files.

What is the build process?

Build process involves in several activities to successfully build a software/ executable from source code.