Compile Process

The compilation process in C programming involves several steps that transform our source code into an executable program that can be run on a computer.

Here's a simplified overview of the typical compilation process:

Create a file with named "firstExample.c"

// firstExample.c

#include <stdio.h>

int main() {
    printf("Hello World, This Program is written in C!\n");
    return 0;
}

Example:

Now, let's compile this program using a typical C compiler like GCC. Here are the steps involved:

Preprocessing:

The preprocessor scans the source code for preprocessor directives (lines starting with #) and performs necessary actions, such as #include, #define, and #ifdef. In this case, it includes the contents of the header file.

It processes these directives, which may include file inclusion, macro substitution, and conditional compilation.

Example:

gcc -E firstExample.c -o firstExample.i

The preprocessor produces an intermediate code known as the preprocessed source code.

Note: This generates an intermediate file "firstExample.i" after preprocessing.

Compilation:

The preprocessed source code is fed into the compiler, which translates the C code into assembly language or an intermediate representation specific to the target platform.

Example:

gcc -S firstExample.i -o firstExample.s

The compiler performs syntax checking, semantic analysis, optimization, and code generation.

It produces an assembly file or object file containing machine code instructions for the target architecture.

Note: This generates an assembly file "firstExample.s".

Example:

gcc -c firstExample.s -o firstExample.o

Assembly (optional):

If the compiler generates assembly code rather than machine code directly, an assembler is used to convert the assembly code into machine code.

The assembler produces an object file containing machine code instructions and other necessary information.

Note: This generates an object file "firstExample.o" containing machine code instructions.

Example:

gcc firstExample.o -o firstExample

Linking:

If your program consists of multiple source files or uses external libraries, the linker is responsible for combining all the object files and resolving references between them.

It resolves symbols (functions and variables) by searching through libraries and object files.

The linker produces a single executable file or library, which contains the complete machine code for the program.

Example:

./firstExample

Note: This generates the executable "firstExample"

Execution:

Finally, the operating system executes the program, starting from the entry point specified in the executable file.

The CPU executes the machine instructions, and the program performs its intended tasks.

Now we can run the compiled program:

Output:

Hello World, This Program is written in C!

Note:

That's the complete compilation process from source code to executable program in C.

The various intermediate files ("firstExample.i", "firstExample.s", "firstExample.o") are produced at different stages of compilation and are typically not needed after the final executable is generated.