Compilation process in c

Compilation process in c

The compilation process for any user-created C program is a series of process sequences, or step-by-step process methods, that convert any C program source code into an executable program file, making it easily runable on any Windows-based hardware machine or computer.

Compilation process in c

Any C program created by a programmer or software developer is typically compiled or stored in a file with the .c extension. Remember, your current computer cannot directly execute this human-readable program source code. It must go through several stages or processes before installation on a computer.

The complete process of program compilation in C programming.

The compilation process for a typical C program can be displayed as follows:

Any C program source code → preprocessor directives → compiler software → assembler software → data linker → executable C file conversion

For example.

testprogram.c

      ↓

Preprocessing Directive

      ↓

testprogram.i

      ↓

Compilation Process

      ↓

testprogram.s

      ↓

Assembly Conversion

      ↓

testprogram.o

     ↓

Linking Data

     ↓

testprogram / testprogram.exe

A basic C program source code.

The first step in the compilation process in C programming is to create the C program source code in Notepad or other Linux, Mac, text editor, or graphical IDE software.

For example.

#include <stdio.h>

int main()

{

printf(“Welcome to Vcanhelpsu”);

return 0;

}

Here, the user-created program is automatically saved with the .c file extension.

For example, welcome.c

In C programming, program source code is created in an easily human-readable order or format, but processor directives cannot directly execute it.

C Programming Preprocessing Directives.

In C programming, the preprocessor directive in program source code is the first step in the compilation process.

It handles or manages all preprocessor directive task actions in C program source code that begin with the # sign.

Some common preprocessor directive examples in C programming.

#include

#define

#ifdef

#ifndef

#endif

For example,

#include <stdio.h>

#define PI 3.14

During preprocessing tasks in C programming.

  • In user-defined C programs, header files are included at the top of the program.
  • This expands macros used in the program.
  • User-defined C program conditional compilation directives are processed.
  • User-defined custom comments in the current program are normally ignored.
  • Other C program preprocessing task operations are performed.
  • The output of a C program is in the form of an expanded source file, traditionally represented by the .i extension in C programs.

welcome.c → Preprocessor → welcome.i

Compilation process in a C program.

  • After the preprocessing directive task or stage, the expanded C program source code is passed to the compiler.
  • The compiler translates or converts the C program source code into assembly language for the target processor.

During this stage, the C program compiler performs the following tasks.

  • Properly checking the syntax of existing C program source code
  • Checking for certain semantic rules in C program source code
  • Optimizing C program source code as needed
  • Translating C program source code statements into lower-level instructions

For example.

if a C program source code contains an invalid statement, such as int total = ;, the compiler displays a compilation error report for that program.

Program output is typically generated as an assembly file with the .s extension.

welcome.i → compiler → welcome.s

Assembly language compiler.

  • The assembler compiler converts the assembly language program source code developed by the compiler into machine language code.
  • Machine program source code contains binary instructions that your computer’s processor (CPU) can directly execute.
  • The assembler creates an object file, typically with the extension .o on Unix or Linux-like operating systems or .obj on the Microsoft Windows toolchain.
  • welcome.s → Assembler → welcome.o
  • An object program source code file contains machine-code instructions, but it may not yet be a complete executable C program.

Linking Process in a C Program

  • The linker links the program code into the next C program source code stage.
  • A C program mostly uses functions from libraries.

For example.

printf(“Welcome to c language”);

printf() is obtained from the standard library in a C program.

The linker combines the following into a C program.

  • Creates object files
  • Includes necessary library code
  • Adds other necessary components to the C program
  • It also resolves references between different parts of the C program.

For example.

welcome.o + C library

     ↓

Linker

    ↓

Executable

If a required function or symbol is not found during this process, the linker may display program errors such as “undefined reference” or “unresolved external symbol.”

Creating an executable file.

After a successful C program linking process, the final C program executable file is developed.

On many Linux-based distro operating systems, the C program executable file may have a different name.

welcome

On Microsoft Windows operating systems, C programs typically have the .exe extension.

welcome.exe

The Windows operating system can load this C program executable file into memory and begin execution.

C program example using the GCC compilation process.

Suppose we have a C program source code named testprogram.c.

We can compile this program using the GCC compiler.

gcc testprogram.c -o testprogram

This command is typically a mandatory task and creates an executable file named testprogram.

This stage can also be viewed differently.

gcc -E testprogram.c -o testprogram.i

Preprocessing task.

gcc -S testprogram.i -o testprogram.s

For compilation into assembly.

gcc -c testprogram.s -o testprogram.o

For assembly process.

gcc testprogram.o -o testprogram

For linking process.

The output of which is the testprogram file, the executable program.

Compilation Process Diagram.

C Program Source Code

          ↓

Preprocessing Directive

          ↓

Expanded Source Code

          ↓

Compilation Process

         ↓

Assembly Source Code Conversion

         ↓

Assembly

         ↓

Object Source Code

         ↓

Linking Process

         ↓

Executable Program Development

         ↓

Final Execution

         ↓

Program Output

Displaying C Program Source Code Errors at Different Stages.

Different types of errors may be displayed or generated during the compilation of a C program source code process.

C program stageCommon error display in c program compilation process
Preprocessing directiveIt displays any missing header file, incorrect macro usages error in c program
Compilation processIt displays any syntax error, type-related error in active c program
Assembly conversionIt checks or analyses invalid assembly instruction in active c program
Linking stageIt checks any undefined reference, missing library in active c program
Final executionIt displays all runtime errors such as division by zero or invalid memory access issues in c file

Conclusion of the Compilation Process in C.

The C program source code compilation process primarily involves four major translation stages.

  • Preprocessing Directives – Processes any C program source code with header files or directives such as #include and #define.
  • Compilation Process – Converts preprocessed C program source code into assembly code.
  • Assembly Translation – Converts assembly program source code into machine-level object code.
  • Linking Process – Combines or merges object code with necessary libraries to convert C program source code into an executable file.

So, the flow of a complete C program source code is as follows.

C program source code (.c) → Preprocessor directive → Compiler process → Assembler translation → Linker process → Creating executable file.

Leave a Reply