Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when I compile?

I wonder what is compiling, what happens when you compile? I mean yes you press compile or type in in the console but what does it actually do in the "background"?

like image 416
ant Avatar asked Sep 14 '09 01:09

ant


People also ask

What happens when you compile?

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.

What does it mean when you compile a program?

Compile refers to the act of converting programs written in high level programming language, which is understandable and written by humans, into a low level binary language understood only by the computer.


2 Answers

  1. First, the compiler "lexes" the source. This means that it transforms the source into a sequence of "tokens." Tokens are sequences of letters, numbers and symbols that have meaning to the compiler.

  2. Next, the compiler "parses" the sequence of tokens from step one. This means that the compiler checks to ensure that the source conforms to rules (the grammar) of the programming language.

  3. Next, the compiler performs syntactic analysis to create a representation of the source to determine the semantical meaning of the source. This is the step where the compiler will build a syntax tree.

  4. Finally, the compiler will generate output that captures the semantic meaning of the source in the target representation (be it machine code, an intermediate language such as Microsoft's CIL, or another programming language).

For the brief details see Wikipedia. For the gory details see the dragon book (every student of computer science should study this book).

like image 64
jason Avatar answered Oct 31 '22 07:10

jason


See this

Basically, magic elves and fairies turn human readable code into machine code.

like image 39
Tom Avatar answered Oct 31 '22 08:10

Tom