Rute kompilasi dan eksekusi
Diperkenalkan
5:52
Dasar Sistem Komputer
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…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
Dalam pelajaran ini
| English | Makna |
|---|---|
| source code/sɔːs kəʊd/ | |
| preprocessor/prɪˈprəʊsesə/ | |
| compiler/kəmˈpaɪlə/ | kompiler |
| assembler/əˈsemblə/ | |
| object file/ˈɒbdʒekt faɪl/ | file objek |
| linker/ˈlɪŋkə/ | penyambung |
| loader/ˈləʊdə/ | pemuat |
| separate compilation/ˈseprət ˌkɒmpɪˈleɪʃn/ | kompilasi terpisah |
| 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.
Preprocessor memeriksa apakah sebuah makro digunakan dengan tipe yang benar.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Preprocessor hanya melakukan penggantian teks. Tipe diverifikasi kemudian, oleh kompiler.
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.
Mengubah main.i menjadi main.s merupakan tugas dari —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Preprocessor menghasilkan .i, kompiler menghasilkan .s, assembler menghasilkan .o, dan linker menghasilkan 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.

Urutkan tahap-tahap pembangunan.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
Ekstensi file menandai tahap-tahap tersebut.
Kesalahan referensi tak terdefinisi berasal dari —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Linker mencocokkan nama di seluruh file objek, sehingga definisi yang hilang merupakan jenis kesalahannya.
Cocokkan setiap kesalahan dengan tahap yang melaporkannya.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Setiap tahap memiliki kelas kesalahannya sendiri, sehingga pesan memberi tahu Anda di mana harus mencari.
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.
Sebuah proyek memiliki 40 file .c. Anda mengedit salah satu di antaranya (bukan header). Berapa banyak file yang harus dikompilasi ulang sebelum pemindaian?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Kompilasi terpisah hanya mengkompilasi ulang file yang diubah; linker kemudian menggabungkan semua file objek 40.
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.
Eksekusi yang dikompilasi dan ditafsirkan berbeda terutama pada —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Kompilasi sekali dan jalankan berkali-kali, atau baca dan jalankan melalui mesin virtual setiap kali.
CPython menjalankan sumber Python tanpa langkah penerjemahan sama sekali.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
CPython pertama-tama mengompilasi sumber menjadi bytecode, lalu mesin virtualnya menjalankan bytecode tersebut.
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.
Apa yang salah? Hanya tim situs yang dapat melihatnya.