Abstract
- Convert from C program to Instruction, then execute it as a Process (进程)
Important
We usually use
gcc
which stands for GNU Compiler Collection, a program that handles the 5 steps program compilation for us.
gcc -Wall program.c
tells the C compilers to give all possible warnings.
Stage 1
- Preprocessor generates expanded
.I
file - Process Pre-processor Directives
- Replace all the Macro Expansion in the Header File & program files
- Would’t complain about missing Header File
- Is done by a program called
cpp
Remove spaces, new lines & comments
Stage 2
- Compiler generates codes in Assembly language in
.s
format - Returns error & warnings if the Header File isn’t defined
- Is done by a program called
cc1
Stage 3
- Assembler generate Instruction in
.o
format - Essential for code sharing and dynamic linking in software development
- Is done by a program called
as
Stage 4
- Linker Link up with other Instruction which are dependencies of the program we wrote
- Sometimes we will break the program into many different
c
files, then run compiler on each, and link up all. This is for easier management & debugging - Output executable format or library file
- We can use
ldd
to show the dependencies - Will return error if unable to link up some of the required
.o
files - Is done by a program called
ld
Stage 5
- Loader loads the executable from Disk to Main Memory to create a Process (进程). This part is handled by the OS
References
- C Language Source Code to EXE
- [Modern OS - 1.8.3 Large Programming Projects](https://csc-knu.github.io/sys-prog/books/Andrew%20S.%20Tanenbaum%20-%20Modern%20Operating%20Systems.pdf#Large Programming Projects)