Header Ads Widget

Compilation Process In C

We Will Discuss About Compilation Process In C

When you write a C program, it needs to be compiled before it can be executed. The compilation process converts the human-readable C code into machine-executable code. In this article, we will take a closer look at the compilation process in C and how it works.

Compilation Process In C



C Programming Language Tutorial

The Compilation Process

The compilation process consists of several stages, each of which performs a specific task. The main stages of the compilation process are:

  1. Preprocessing
  2. Compilation
  3. Assembly
  4. Linking

Let’s take a closer look at each of these stages.

  1. Preprocessing

The first stage of the compilation process is preprocessing. In this stage, the preprocessor scans the source code and performs several tasks, such as:

  • Expanding macros: Macros are preprocessor directives that are used to define constant values or functions. The preprocessor replaces the macro with its corresponding value or function definition.
  • Including header files: Header files contain function prototypes and constant definitions that are needed by the C program. The preprocessor includes the header files in the source code.

Here is an example of a preprocessor directive:

#include <stdio.h>

This directive tells the preprocessor to include the standard input/output library in the program.

  1. Compilation

The second stage of the compilation process is compilation. In this stage, the compiler takes the preprocessed source code and translates it into assembly language. Assembly language is a low-level programming language that is specific to a particular processor architecture.

The compiler performs several tasks during the compilation stage, such as:

  • Checking syntax and semantics: The compiler checks the syntax and semantics of the C code to ensure that it is valid and can be executed.
  • Generating intermediate code: The compiler generates intermediate code that is used by the next stage of the compilation process.

Here is an example of a C program:

#include <stdio.h>

int main() {
printf("Hello, World!");
return 0;
}

When this program is compiled, the compiler generates assembly language code that looks like this:

.file "hello.c"
.section .rodata
.LC0:
.string "Hello, World!"
.text
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
popq %rbp
ret

This assembly code is specific to the processor architecture that the program is compiled on. The assembly code is then assembled into machine language code that can be executed by the processor.

C Programming Language Tutorial

  1. Assembly

The third stage of the compilation process is assembly. In this stage, the assembler takes the assembly language code generated by the compiler and assembles it into machine language code. Machine language code is a binary code that is specific to the processor architecture.

The assembler performs several tasks during the assembly stage, such as:

  • Converting assembly language code to machine language code: The assembler converts the assembly language code into machine language code that can be executed by the processor.
  • Generating object files: The assembler generates object files that contain the machine language code and other information about the program.

Here is an example of an object file:

hello.o: file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <main>:
0: 48 83 ec 08 sub $0x8,%rsp
4: 48 c7

  1. Linking

The fourth and final stage of the compilation process is linking. In this stage, the linker takes the object files generated by the assembler and links them together to create an executable program.

The linker performs several tasks during the linking stage, such as:

  • Resolving external references: The linker resolves external references between object files. For example, if one object file calls a function that is defined in another object file, the linker resolves this reference by linking the two object files together.
  • Assigning memory addresses: The linker assigns memory addresses to the program and its data. This allows the program to be loaded into memory and executed by the processor.

Here is an example of a linker command:

gcc -o hello hello.o

This command tells the linker to create an executable program called “hello” from the object file “hello.o”.

The compilation process can be complex and involve multiple steps, but it is necessary to convert human-readable C code into machine-executable code. By understanding the compilation process, you can write more efficient and error-free C programs.

Example

Let’s take a simple example to understand the compilation process in C. Here is a C program that calculates the sum of two numbers:

#include <stdio.h>

int main() {
int a = 10, b = 20;
int sum = a + b;
printf("The sum of %d and %d is %d", a, b, sum);
return 0;
}

To compile this program, we need to perform the following steps:

  1. Preprocessing: We use the following command to preprocess the program:
gcc -E sum.c -o sum.i

This command generates a preprocessed file called “sum.i”. This file contains the expanded macros and included header files.

  1. Compilation: We use the following command to compile the program:
gcc -S sum.i -o sum.s

This command generates an assembly language file called “sum.s”. This file contains the translated C code.

  1. Assembly: We use the following command to assemble the program:
gcc -c sum.s -o sum.o

This command generates an object file called “sum.o”. This file contains the assembled machine code.

  1. Linking: We use the following command to link the program:
gcc sum.o -o sum

This command generates an executable program called “sum” that can be executed.

Conclusion

The compilation process in C is a crucial step in converting human-readable C code into machine-executable code. The process involves several stages, including preprocessing, compilation, assembly, and linking. By understanding the compilation process, you can write more efficient and error-free C programs.

Yhaa You have done it but next? if YOU Want to your Others Programming Then Follow US HERE and Join Telegram.

Post a Comment

0 Comments