Compilation, Linking and Execution
In questa pagina 5
Interpretation vs compilation
The computer only executes machine language (architecture dependent), so a translator is needed.
- Interpreter: translates a high-level instruction and executes it immediately.
- Compiler: translates all the instructions together into machine language, then they are executed directly (C, C++).
- Intermediate solution: compile to bytecode and interpret it (Java).
| Interpreter | Compiler | |
|---|---|---|
| Execution | slower | faster (code usually optimized too) |
| Translator at run time | required | not required |
| After a change | just run again | recompile |
| Portability | source + translator runs anywhere | must be compiled for each architecture |
C is a compiled language: a restricted set of basic commands (many library functions), so the compiler is "easy" to write and therefore quite portable. Python and JavaScript typically use interpreters (fast testing cycles, flexible, slower).
The four phases
EDITOR -> PREPROCESSOR -> COMPILER -> LINKER -> executable
.c (text) object code + libraries- Editor: the programmer writes the program and stores it on disk.
- Preprocessor: processes the text.
- Removes comments.
- Every line starting with
#is a directive.#include <x>: the contents of file x are copied at that point (this gives access to the library's commands, e.g.stdio.hallowsprintf). - Expands macros:
#define X 3replaces every occurrence ofXin the file with 3 (see Control StructuresBasic syntax rules and the control structures of C: if/else, switch, the ternary operator, goto, symbolic constants and the while, for and do-while loops with break and continue.Control Structures → for symbolic constants).
- Compiler: analyzes the code and translates it into low-level instructions, stored as object code on disk.
- Instructions must strictly follow the C syntax; if the compiler cannot parse the code, an error is generated.
- Stages: lexical analysis -> syntax analysis -> semantic analysis -> optimization and code generation.
- Linker: links the object code to the libraries and creates the executable file.
- A program is usually made of many files and uses functions written by others (e.g.
printf). - To avoid duplicating that code, such functions are loaded in memory once and linked to our program.
- The linker receives the (compiled) file that uses
printfand the file whereprintfis defined. - It combines object files into one executable, resolves symbol references (function calls, variable accesses) across modules, and supports modular and reusable development.
- A program is usually made of many files and uses functions written by others (e.g.
Static vs dynamic linking
| Static | Dynamic | |
|---|---|---|
| Library code | embedded in the executable at build time | loaded at run time |
| Executable size | larger | smaller |
| Runtime dependencies | fewer | needs the library present |
| Library updates | need rebuild | possible without recompiling |
Both have trade-offs in performance, flexibility and maintenance. Building and using libraries is treated later in the course.
Compiling and running on Linux
gcc helloWorld.c -o helloWorld # C source: .c extension
./helloWorld # prints: Hello world!
g++ helloWorld.cpp -o helloWorld # C++ source: .cpp extension, g++ instead of gcc- If there are no errors in the source, an executable named
helloWorldis created (-ogives the output name); other systems are "more or less very similar". - Where the output goes: to the standard output, the screen by default.
Reading compiler errors
Slide examples (Visual Studio style codes), each from a deliberate mistake in Hello world:
;after the#includeline: warning C4067, unexpected tokens after preprocessor directive.- Missing
{afterint main(): error,{expected. - Missing closing
}: error C1075, no matching token for{. "Hello World!\c\n": warning C4129, unknown escape sequencec.- Missing
#include <stdio.h>: error thatprintfis not defined (C++: namespacestdhas nocout).
Check yourself (slide questions)
What is a compiler; how compiled code differs from source; which tool produces source (the editor) and which convert it (preprocessor, compiler, linker); does the linker work on source or machine code (machine/object code); what the preprocessor does to source (comments, directives, macros).
Versione ripasso
- Interpreter: translate and run each instruction (slower, needs translator, portable source). Compiler: translate everything once (faster, recompile after each change, one build per architecture). C = compiled; Java = bytecode + interpretation.
- Pipeline: editor -> preprocessor -> compiler -> linker.
- Preprocessor: strips comments,
#includecopies the file in place,#define X 3replaces X with 3. - Compiler: lexical, syntax, semantic analysis, optimization and code generation, produces object code; syntax errors stop here.
- Linker: object files + libraries -> executable; resolves symbols such as
printf.
- Preprocessor: strips comments,
- Static linking: code embedded, bigger file, fewer dependencies. Dynamic: loaded at run time, smaller file, library update without recompiling.
- Commands:
gcc helloWorld.c -o helloWorld
g++ helloWorld.cpp -o helloWorld
./helloWorld- Typical errors:
;after a directive, unmatched braces, unknown escape\c, missing#include. - See Introduction to C and the Development WorkflowWhat a computer and a program are, how algorithms are specified with preconditions and postconditions, why C is a compiled language, and the structure of a first C program.Introduction to C and the Development Workflow → for the first program.