תהליך הקומפילציה והביצוע
הוצג
5:52
יסודות במחשבים
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…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| preprocessor/prɪˈprəʊsesə/ | מעבד מקדים |
| compiler/kəmˈpaɪlə/ | מתרגם |
| assembler/əˈsemblə/ | אסמבלר |
| object file/ˈɒbdʒekt faɪl/ | קובץ מטרות |
| linker/ˈlɪŋkə/ | מחבר |
| loader/ˈləʊdə/ | טוען |
| separate compilation/ˈseprət ˌkɒmpɪˈleɪʃn/ | הרכבה נפרדת |
| 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.
המערבך בודק שהמאקרו משמש עם הסוגים הנכונים.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
המערבך מבצע החלפת טקסט בלבד. הסוגים נבדקים מאוחר יותר על ידי המעבד.
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.
המרה של main.i ל-main.s היא משימת —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
המערבך יוצר .i, המעבד יוצר .s, המאסמבלר יוצר .o והמקשר יוצר את התוכנית.
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.

סדר את שלבי הבנייה.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
הסיומות בשם הקובץ מציין את השלבים.
שגיאת רפרנס לא מוגדר נובעת מ—
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
המקשר תואם שמות בין קבצי אובייקט, ולכן היעדר הגדרה הוא סוג שגיאה.
תאם כל שגיאה לשלב שמדווח עליה.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
לכל שלב יש קטגוריית שגיאות משלו, כך שהודעת השגיאה מצביעה על המקום לבדוק.
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.
לפרויקט יש 40 קבצי .c. ערכת אחד מהם (ולא קובץ ראש). כמה קבצים יש למתרגם מחדש לפני המקשר?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
מתג הרכבה מחזר רק את הקובץ ששתנה; לאחר מכן המקשר מאחד את כל קבצי האובייקט 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.
ביצוע ממוחשב וביצוע מפורש נבדלים בעיקר ב—
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
לתרגם פעם אחת ולהריץ פעמים רבות, או לקרוא ולהריץ דרך מכונה וירטואלית בכל פעם.
CPython מריץ קוד Python ללא שלב מתג כלל.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
CPython מתרגמת תחילה את הקוד המקור לביטוי ביטוני, ולאחר מכן המכונה הווירטואלית שלה מפעילה את הביטוי הביטוני.
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.
מה השתבש? רק צוות האתר רואה זאת.