2_compilation and linking c program
2_compilation and linking c program
Linking
C -Program
Definition?
• Compilation refers to the process by which human-readable source
code written in any programming language is transformed into
machine-executable binary code/ object code.
• The compiler checks the source code for the syntactical or structural
errors, and if the source code is error-free, then it generates the object
code.
• Translation to Machine Code: Computers operate using binary machine language code, and source code written in C
is in a human-readable format. Compilation is the process that translates C source code into machine code for
execution.
• Error Checking: Compilation checks for syntax errors, type mismatches, and other issues, which can save developers
significant time in debugging and result in more robust programs.
• Optimization: Compilers apply various optimization techniques to the generated code to improve program efficiency
and performance.
• Modularity: Compilation in C allows for the modularity of large programs by dividing them into separate source files.
Linking combines these files into a unified program.
• Portability: C code is typically written to be platform-independent. Compilation ensures that the code can run on
different platforms without modification.
• Efficient Execution: Compiled code executes faster than interpreted code because it is already in machine code form,
requiring no parsing or interpretation during execution.
• Independence from Source Code: Once compiled, a C program can be executed without the original source code,
protecting intellectual property.
• Safety and Security: Compilation in C can help identify security vulnerabilities at an early stage, thus reducing the
risk of security breaches.
• The compilation process in C
can be divided into several
phases, each with specific
tasks and objectives.
1. Pre-processing
2. Compilation
3. Assembling
4. Linking
Step 1: Preprocessor
• The file extension (.c) is used to identify source code files, which are text files
with code written in them. This source code is first given to the preprocessor,
who expands it from there. After the code is extended, it is handed to the
compiler for further processing. This phase includes the following-
• Comments Removal:
• The comments are the portion of code that the pre-processor removes
during compilation because they are not very useful to the machine. They
are used in C programs to provide a general understanding of a particular
statement or section of code. When the pre-processing stage is over, the
comments in the program below will be deleted.
• Macros Expansion:
• A macro in C programming is a section of code that has a given name and can be used repeatedly throughout a
program. The #define preprocessor directive links a name to a value or a section of code and is used to define
macros.
• Before the program is compiled, the preprocessor substitutes the macro name with the appropriate value or
code when it is used in the program. Note that using macros can make code simpler, easier to read, and
require less typing.
• File Inclusion:
• When pre-processing a C program, file inclusion refers to adding a different file containing some pre-written
code. The #include directive is utilized here.
• File inclusion during pre-processing allows the #include<filename> directive to be replaced with the whole
contents of the filename. Thus, the filename's content is added to the source code, producing a new
intermediate file.
• Example- We can integrate the <stdio.h> header file by inserting the #include directive at the beginning of our
code. Contained within this header file are standard input/output functions, among which include the highly
utilized scanf() and printf().
Step 2: Compilation
• The Compilation phase in C involves translating the preprocessed source code into assembly or machine code and
generating object files. Here's a breakdown of the actions that occur during this phase:
• Translation:
• The compiler translates the preprocessed C source code into an intermediate representation, typically assembly
language or an intermediate machine-independent code.
• The translation process involves analyzing the code for syntax errors and generating corresponding machine-level
instructions.
• Code Optimization:
• The compiler may perform various optimizations to improve the efficiency of the generated code.
• Optimization techniques include constant folding, loop unrolling, and inlining functions to reduce execution time or
memory usage.
• Generation of Object Files:
• The result of the compilation phase is one or more object files (with a .o extension on Unix-like systems or .obj on
Windows).
• Object files contain machine code specific to the target architecture and may also include additional information,
such as symbols and relocation entries.
Step 3: Assembler
• The compiler sends the assembly code (i.e., a simple English language
used to write low-level instructions) to the assembler, which then
turns it into object code. In other words, the assembler helps in
converting the assembly file into an object file containing machine-
level code. In this step, the file's extension changes to (.obj).
Step 4: Linking
• A linker is a tool used to connect every component of a program in the
correct execution order. After the code has gone through this phase/ stage
of compilation in C, it is in the executable machine code format. Note that if
a program's code proves to be excessively lengthy, it may be necessary to
break its contents into two distinct documents. These divided segments can
then undergo separate construction processes before ultimately converging
via an intermediary module known as the Linker.
• The linker is a crucial component of the C language compilation process.
Suppose your C program features a header file and integrates any function
specified within this essential script. In that case, the linker shall proceed to
bundle together both your object code and every pertinent library-based
counterpart.
• The linking process can be further categorized into two main types:
• Static Linking: In static linking, the linker combines object files and libraries into a
single executable file. The resulting program contains all the required code and
data, making it independent of external libraries. This means that the entire
program, including library code, is bundled into the executable file. The advantage
is that the program is self-contained, but it may result in larger executable files.
• Dynamic Linking: In dynamic linking, the linker includes only references to external
libraries in the executable, not the actual library code. The operating system's
dynamic linker/loader loads the necessary library code into memory at runtime.
This approach allows for smaller executable files and makes it easy to share library
code among multiple programs. Some common dynamic library file formats include
DLL (Dynamic Link Library) on Windows and shared objects (.so) on Unix-like
systems.