Skip to content

Compiler

Also called: compilers

Published:

What stages does a compiler go through?

According to the GCC documentation, compilation can involve up to four stages: preprocessing, compilation proper, assembly and linking, always in that order [1]. Command-line options decide where the process stops. -E stops after preprocessing, -S stops after compilation proper and leaves an assembler file, and -c compiles or assembles each source file into an object file without linking [1].

How does a C file become a runnable program?

Run gcc hello.c -o hello with no stop option and all four stages run, with -o placing the primary output in a file called hello [1]. Run gcc -S hello.c on the same file and you get an assembler file instead of an executable [1]. Opening that intermediate file is the most direct way to see which instructions the compiler chose for your C code.

Do compilers only produce machine code?

No. Python source is compiled too, but into bytecode rather than machine code. Bytecode is the internal representation of a Python program in the CPython interpreter, and it is cached in .pyc files so the same file runs faster the second time [2]. This intermediate language runs on a virtual machine [2]. The dis module disassembles it into readable form, and its documentation stresses that bytecode is an implementation detail that can change between Python releases [3].

Frequently asked questions

What is the difference between gcc -c and gcc -S?

-c produces an object file without linking. -S stops after compilation proper and leaves an assembler file.

Is Python a compiled language?

Python is usually called interpreted, but its source is first compiled to bytecode, so the line is blurry.

Sources

  1. Overall Options (Using the GNU Compiler Collection) — GCC documentation
  2. Glossary — Python documentation
  3. dis — Disassembler for Python bytecode — Python documentation