Object File – Definition
An object file is an intermediate file generated by the compiler after successful compilation of a C source file.
It contains machine-level instructions, but it is not executable.
Typical extensions:
-
.o (Linux/Unix)
-
.obj (Windows)
Contents of an Object File
An object file generally contains:
-
Machine Code
-
Symbol Table
-
Relocation Information
-
Data Section
Types of Object Files
-
Relocatable Object File
-
Executable Object File
-
Shared Object File
-
Used for dynamic linking
-
Example: .so, .dll
Linker – Definition
A linker is a system software tool that combines one or more object files and required libraries to produce a final executable file.
Functions of the Linker
1. Symbol Resolution
2. Address Binding
3. Combining Object Files
Example:
gcc file1.o file2.o
4. Library Linking
Types of Linking
1. Static Linking
-
Library code is copied into executable
-
Executable size is larger
-
No external dependency at runtime
Example:
gcc -static prog.c
2. Dynamic Linking
Example:
gcc prog.c
Linking Errors
Errors detected during linking are called linker errors.
Examples:
Example error:
undefined reference to `add`
Difference Between Compiler and Linker
| Compiler |
Linker |
| Converts source code to object code |
Combines object files |
| Checks syntax errors |
Resolves external symbols |
Produces .o file |
Produces executable |
| Works on one file at a time |
Works on multiple files |
Program Flow (Simplified)
Source File (.c)
↓
Compiler
↓
Object File (.o)
↓
Linker
↓
Executable File