The hello program starts as a high-level C program, which is easy for humans to read and understand. However, to actually run hello.c
on a computer, the C code needs to be translated into low-level machine-language instructions. These instructions are then combined into a special format called an "executable object program" and saved as a binary file. These are also known as "executable object files."
On a Unix system, the process of converting the source file into an object file is done by a compiler driver. For example, you can use the gcc
command like this:
unix> gcc -o hello hello.c
This command compiles the hello.c
file and creates an executable file named hello
.
The compilation system.
When you use the gcc
compiler to create an executable file from hello.c
, it goes through four main steps. These steps are handled by different programs that together make up the compilation system: "the preprocessor", "compiler", "assembler", and "linker".
Here's how it works:
Preprocessing Phase:
- The preprocessor (
cpp
) starts by modifying the original C program based on commands that start with#
. For example,#include <stdio.h>
inhello.c
tells the preprocessor to include the contents of thestdio.h
file into your program. After preprocessing, you get a modified C program, often with a.i
extension.
- The preprocessor (
Compilation Phase:
- Next, the compiler (
cc1
) takes the preprocessed file (hello.i
) and translates it into an assembly-language program (hello.s
). Assembly language is a low-level code that represents machine instructions in a human-readable form. This step is important because it provides a common language for different compilers.
- Next, the compiler (
Assembly Phase:
- The assembler (
as
) then converts the assembly code (hello.s
) into machine-language instructions. These instructions are stored in a file calledhello.o
, which is known as a relocatable object file. This file is in binary format, so if you open it with a text editor, it will look like nonsense.
- The assembler (
Linking Phase:
- The linker (
ld
) takes thehello.o
file and combines it with other necessary object files, such asprintf.o
, which contains theprintf
function. The linker merges these files to create the final executable file (hello
). This executable file can now be loaded into memory and run by the system.
- The linker (
These steps ensure that your high-level C code is properly converted into a format that your computer can execute.