Ruta de compilación y ejecución
Introducido
5:52
Fundamentos de sistemas informáticos
This course follows a program from a single bit to a running process. The first two units are the alphabet: how bits encode data, and how logic gates compute…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| source code/sɔːs kəʊd/ | |
| preprocessor/prɪˈprəʊsesə/ | preprocesador |
| compiler/kəmˈpaɪlə/ | compilador |
| assembler/əˈsemblə/ | ensamblador |
| object file/ˈɒbdʒekt faɪl/ | archivo objetivo |
| linker/ˈlɪŋkə/ | enlazador |
| loader/ˈləʊdə/ | cargador |
| separate compilation/ˈseprət ˌkɒmpɪˈleɪʃn/ | compilación separada |
| bytecode/ˈbaɪtkəʊd/ |
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.
El preprocesador verifica que una macro se use con los tipos correctos.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
El preprocesador solo intercambia texto. Los tipos se verifican después, por el compilador.
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.
Convertir main.i en main.s es tarea de —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
El preprocesador genera .i, el compilador genera .s, el ensamblador genera .o y el enlace genera el programa.
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.

Ordena las etapas de construcción.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Las extensiones de archivo nombran las etapas.
Un error de referencia no definida proviene de —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
El enlazador hace coincidir los nombres entre archivos objeto, por lo que una definición ausente es su tipo de error.
Asocia cada error con la etapa que lo informa.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Cada etapa tiene su propia clase de error, por lo que el mensaje indica dónde buscar.
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.
Un proyecto contiene 40 archivos .c. Editas uno de ellos (no un encabezado). ¿Cuántos archivos deben recompilarse antes del enlace?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
La compilación separada recompila solo el archivo modificado; luego el enlazador une todos los 40 archivos objeto.
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.
La ejecución compilada e interpretada difieren principalmente en —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Compilar una vez y ejecutar muchas veces, o leer y ejecutar a través de una máquina virtual cada vez.
CPython ejecuta código fuente de Python sin ninguna etapa de traducción.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
CPython primero compila el código fuente a bytecode, luego su máquina virtual ejecuta el 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
- Each stage has its own output and its own errors; then the loader starts the program:
| Stage | Makes | Errors |
|---|---|---|
| Preprocessor | main.i (text) |
bad macros |
| Compiler | main.s (assembly) |
syntax, types |
| Assembler | main.o (object) |
bad instructions |
| Linker | executable | undefined reference |
- 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.
¿Qué salió mal? Solo el equipo del sitio ve esta información.