How does a compiler work? That is the question. A Compiler’s main task if to turn your source code into an executable file that computer understands. To achieve this, a compiler has to undergo several stages to build the final executable. The picture below describes difference compilation stages:
On a Linux based environment, a common compiler is called gcc for C or g++ for C++ programs. On a Windows based environment, MinGW is a common compiler to use.
The Compiler Works in 4 Stages
Most executable programs are produced in these 4 stages of compilation.
- The first stage in the compilation process.
- Expand preprocessor directives starting with a ‘#’ symbol and include commands like
#ifdef. See more details about preprocessor clauses here.
- Performs text substitutions, file inclusions, and conditional compilation…etc
- Removes comments, resolves macros, and expands header files included via
- The output of the preprocessor is an expanded source code file.
- The compiler takes the preprocessed source code as input and translates it into assembly code or an intermediate representation.
- It performs lexical analysis (tokenizing), syntax analysis (parsing), and semantic analysis (type checking).
- The compiler generates machine-independent assembly code.
- Some compilers may perform optimizations at this stage to improve the efficiency of the generated code.
- The output of this stage is typically in the form of assembly code (which is still human readable).
- Responsible for translating assembly code into machine code. (This is what a CPU understands).
- It performs assembly-level optimizations, such as instruction scheduling for a particular CPU type.
- The assembler generates an object file containing the machine code and information about program sections (data, text, etc.).
- The object file is typically in a (.o) format on Unix-based operating system.
- The final stage of compilation, responsible for combining multiple object files and libraries into a single executable program.
- It resolves references between different object files and ensures that functions and variables declared in one file are correctly linked to their definitions in other files.
- The linker also resolves dependencies on external libraries and system functions.
- It produces the final
- Additionally, the linker can perform optimizations and generate debug information for a debugger to trace. See more about using a debugger like GDB here.
See below picture for a quick summary that answers question: “how does a compiler work”.
Hi, this is Cary, your friendly tech enthusiast, educator and author. Currently working as a software architect at Highgo Software Canada. I enjoy simplifying complex concepts, diving into coding challenges, unraveling the mysteries of software. Most importantly, I like sharing and teaching others about all things tech. Find more blogs from me at highgo.ca