Salta al contenuto
Note per Studenti Compilation, Linking and Execution Report

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
  1. Editor: the programmer writes the program and stores it on disk.
  2. Preprocessor: processes the text.
  3. 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.
  4. 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 printf and the file where printf is defined.
    • It combines object files into one executable, resolves symbol references (function calls, variable accesses) across modules, and supports modular and reusable development.

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

bash
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 helloWorld is created (-o gives 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 #include line: warning C4067, unexpected tokens after preprocessor directive.
  • Missing { after int main(): error, { expected.
  • Missing closing }: error C1075, no matching token for {.
  • "Hello World!\c\n": warning C4129, unknown escape sequence c.
  • Missing #include <stdio.h>: error that printf is not defined (C++: namespace std has no cout).

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, #include copies the file in place, #define X 3 replaces 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.
  • Static linking: code embedded, bigger file, fewer dependencies. Dynamic: loaded at run time, smaller file, library update without recompiling.
  • Commands:
bash
gcc helloWorld.c -o helloWorld
g++ helloWorld.cpp -o helloWorld
./helloWorld

Teoria collegata