Compilation and execution path
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| preprocessor/prɪˈprəʊsesə/ | preprocessor |
| compiler/kəmˈpaɪlə/ | Software that translates a program from a source language into another form, commonly machine or intermediate code. |
| assembler/əˈsemblə/ | Software that translates assembly-language instructions into machine code. |
| object file | object file |
| linker/ˈlɪŋkə/ | A tool that combines object code and resolves references between compiled modules or libraries. |
| loader | loader |
| separate compilation | separate compilation |
| bytecode/ˈbaɪtkəʊd/ | Intermediate instructions intended for execution by a virtual machine or interpreter rather than directly by a physical processor. |
It compiled, so why did it fail?
- A student builds a program and gets
undefined reference to 'area'. The code had no errors when it compiled, so which tool is complaining? - Turning source code into a running program takes four stages, and each stage has its own kind of error. Knowing the stages tells you where to look.
Stage 1: the preprocessor
- The preprocessor 预处理器 expands
#includeand#define. It works on plain text and producesmain.i. - Macros have no types: the preprocessor only swaps text, so a bad macro shows up as a strange error later.
The preprocessor checks that a macro is used with the right types.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The preprocessor only swaps text. Types are checked later, by the compiler.
Stage 2: the compiler
- The compiler 编译器 checks types and turns C into assembly language,
main.s. - Type errors and syntax errors come from this stage.
Turning main.i into main.s is the job of —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The preprocessor makes .i, the compiler makes .s, the assembler makes .o and the linker makes the program.
Stage 3: the assembler
- The assembler 汇编器 turns assembly into machine code in an object file 目标文件,
main.o. - A call to a function in another file stays unresolved: the object file just records the name it needs.
Stage 4: the linker, then the loader
- The linker 链接器 joins object files and libraries and matches each needed name to its definition. A name with no definition gives "undefined reference": a linker error, not a compiler error.
- The result is an executable. The loader 加载器 maps it into memory and starts it as a process.

Put the build stages in order.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The file extensions name the stages.
An undefined-reference error comes from —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The linker matches names across object files, so a missing definition is its kind of error.
Match each error to the stage that reports it.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Each stage has its own error class, so the message tells you where to look.
Why keep the stages apart
- Separate compilation 分别编译: change one file in a million-line program and only that file is compiled again; then the linker joins everything.
- Libraries can ship as ready-made object files, and every error belongs to one stage: macro, type, instruction or symbol.
A project has 40 .c files. You edit one of them (not a header). How many files must be compiled again before linking?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Separate compilation recompiles only the changed file; the linker then joins all 40 object files.
Compiled and interpreted
- C goes through all four stages before it runs. The result is fast but tied to one kind of machine.
- Python reads the source each run: CPython compiles it to bytecode 字节码, and a virtual machine runs that. The difference is when translation happens.
Compiled and interpreted execution differ mainly in —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Compile once and run many times, or read and run through a virtual machine every time.
CPython runs Python source with no translation step at all.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
CPython first compiles the source to bytecode, then its virtual machine runs the bytecode.
Worked example: the four files
main.cbecomesmain.i(preprocess),main.s(compile),main.o(assemble), thenprog(link). Each file can be kept and inspected with the right compiler flag.- In the opening story,
areawas defined ingeom.c, but onlymain.owas linked. Linkmain.oandgeom.otogether and the error disappears.
Three common slips
- Calling every failure a compile error. "Undefined reference" comes from the linker.
- Forgetting that headers are text.
#includepastes the file in; macros have no types. - Assuming optimisation never changes float results. Some optimisations reorder float arithmetic, and rounding then changes.
Recap
- Preprocessor (text) to compiler (types) to assembler (machine code) to linker (symbols); then the loader starts it.
- Undefined reference means a linker problem: a missing object file or library.
- Compiled or interpreted is about when translation happens: once before running, or every run.