Understanding Compilation process and linking

In the world of programming, writing code is just the beginning. Before a program can be run, it needs to be translated from human-readable code into machine-executable instructions. This process is known as compilation. In the case of C programming language, understanding how compilation works is essential for every programmer.

What is Compilation?

Compilation is the process of translating source code written in a high-level programming language like C into machine code, which can be understood and executed by a computer’s processor.

Why Do We Need Compilation?

Computers can only understand machine code, which consists of binary instructions (0s and 1s). Since it’s impractical for humans to write programs directly in machine code, high-level languages like C are used, which offer better readability and maintainability. Compilation bridges the gap between human-readable code and machine-executable code.

Compilation Steps

Preprocessing

Before the actual compilation begins, the preprocessor steps in. It handles directives such as #include, #define, and #ifdef, and performs text substitution as per the directives.

				
					// Example of preprocessing
#include <stdio.h>

#define MAX 100

int main() {
    printf("The maximum value is: %d\n", MAX);
    return 0;
}

				
			

Compilation

During this stage, the preprocessed code is translated into assembly code specific to the target architecture. This assembly code is still human-readable but is much closer to the machine code than the original C code.

Assembly

In this step, the assembly code generated in the previous step is translated into machine code. This machine code consists of binary instructions that can be directly executed by the computer’s processor.

Linking

Once all source files have been compiled into object files, the linker combines them together to form the final executable. It resolves external references, such as function calls to library functions, and ensures that all necessary code and data are present.

Compilation Example

Let’s consider a simple C program and see how it goes through the compilation process

				
					// File: hello.c
#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}
				
			
				
					gcc -o hello hello.c

				
			
				
					// output //
Hello, world!

				
			

What is Linking?

Linking is the process of combining multiple object files and libraries to create a single executable or shared library. It resolves references between different modules and ensures that all symbols are correctly defined.

Types of Linking

Static Linking

In static linking, all necessary code from libraries is copied into the final executable. This results in a standalone executable file that does not depend on external libraries.

Dynamic Linking

In dynamic linking, references to external libraries are resolved at runtime. The executable file contains references to shared libraries, and these libraries are loaded into memory when the program starts.

Linking Process

Symbol Resolution

During linking, the linker resolves symbols by matching references in one object file with definitions in other object files or libraries.

Relocation

The linker adjusts memory addresses in the object files to reflect the final memory layout of the executable. This process is known as relocation.

Symbol Table

The linker maintains a symbol table, which maps symbols to their memory addresses. This table is used during symbol resolution and relocation.

Linking Example

Consider a program that uses a function add defined in another source file add.c.

File – main.c

				
					#include <stdio.h>

int add(int a, int b); // Function declaration

int main() {
    int result = add(3, 4);
    printf("Result: %d\n", result);
    return 0;
}

				
			

File – add.c

				
					int add(int a, int b) {
    return a + b;
}

				
			

Compilation Commands

				
					gcc -c main.c -o main.o
gcc -c add.c -o add.o
gcc -o program main.o add.o

				
			
				
					// output //
Result: 7

				
			

Compilation and linking diagram

				
					        +---------------------+               +--------------------------+
        | Source Code (.c)    |               | Header Files (.h)        |
        +---------------------+               +--------------------------+
                 |                                  |
                 |                                  |
                 V                                  V
        +---------------------+     Preprocessing    +--------------------------+
        | Preprocessed        | ------------------->| Preprocessed             |
        | Source Code (.i)    |                      | Header Files (.i)        |
        +---------------------+                      +--------------------------+
                 |                                  |
                 |                                  |
                 V                                  V
        +---------------------+     Compilation     +--------------------------+
        | Compiled Object     | ------------------->| Compiled Object          |
        | Files (.o)          |                      | Files (.o)               |
        +---------------------+                      +--------------------------+
                 |                                  |
                 |                                  |
                 V                                  V
        +---------------------+                     +---------------------------+
        |                     |    Linking          |                           |
        | Executable Program  |<------------------->| Libraries and            |
        | (.exe or .out)      |                     | External Object Files    |
        +---------------------+                     +---------------------------+

				
			

How C program Runs ?

				
					+-----------------------+  +-----------------------+  +-----------------------+  +-----------------------+
|                       |  |                       |  |                       |  |                       |
|    Source Code (.c)   |  |   Preprocessed        |  |   Compiled Object     |  |   Executable Program  |
|                       |  |   Source Code (.i)    |  |   Files (.o)          |  |   (.exe or .out)      |
|                       |  |                       |  |                       |  |                       |
+-----------------------+  +-----------------------+  +-----------------------+  +-----------------------+
      |                             |                             |                             |
      |      Preprocessing          |         Compilation         |            Linking          |
      +-----------------------------+-----------------------------+-----------------------------+

				
			

Understanding the compilation process and linking is crucial for every C programmer. It enables you to write efficient, maintainable code and debug issues that arise during compilation and linking stages. By mastering these concepts, you can unleash the full potential of the C programming language and build robust, high-performance applications.Happy coding!❤️

Table of Contents