Home  C-language   C program c ...

C program compilation process

The C code compilation process involves several stages that transform human-readable source code into machine-executable binary code. Here's a detailed explanation of each stage in the compilation process:

1. Preprocessing

Example:

Source file (main.c):

#include <stdio.h>
#define MAX 100

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

Preprocessed file (main.i):

// Content of stdio.h included here
int main() {
    printf("MAX is %d\n", 100);
    return 0;
}

2. Compilation

Example:

Preprocessed file (main.i):

int main() {
    printf("MAX is %d\n", 100);
    return 0;
}

Assembly file (main.s):

    .section    __TEXT,__text,regular,pure_instructions
    .macosx_version_min 10, 15
    .globl  _main
    .p2align    4, 0x90
_main:
    pushq   %rbp
    movq    %rsp, %rbp
    leaq    L_.str(%rip), %rdi
    movl    $100, %esi
    xorl    %eax, %eax
    callq   _printf
    xorl    %eax, %eax
    popq    %rbp
    retq
L_.str:
    .asciz   "MAX is %d\n"

3. Assembly

Example:

Assembly file (main.s):

.section __TEXT,__text,regular,pure_instructions
...

Object file (main.o):

4. Linking

Example:

Object files (main.o, stdio.o):

Linked executable (main or main.exe):

5. Loading and Execution

Example:

Executable (main):

$ ./main
MAX is 100
Published on: Jun 25, 2024, 07:54 AM  
 

Comments

Add your comment