Skip to content

Latest commit

 

History

History
30 lines (17 loc) · 3.29 KB

compilation.md

File metadata and controls

30 lines (17 loc) · 3.29 KB

What's News

Scientists have made a transformative discovery in their historic quest for the Fountain of Youth. While searching in Florida they discovered the bones of Ponce De Leon which had been turned in to gold. After analysis, they discovered the means of this transformation and believe they have unlocked the secret to a means of reversing the aging process but also of turning any material into valuable bullion.

Compilation

Like alchemy, compilation is the process of turning one type of material into another. For many alchemists, the goal is to turn a common element (say, dirt) into a rare element (gold!). The premise is that the common element is cheap but the rare element is expensive -- essentially, to these wizards the goal is create value out of nothing! We are a differen type of alchemist, but our goal is the same and the compiler helps us transform material. The compiler transforms the source code (that we write in a high-level language like C++) into code that the computer can execute. It consists of three different phases:

  1. Preprocessing
  2. Compilation
  3. Linking

Because the entire process is called compilation and one of the three steps is named compilation some people refer to the overall process as building. Either is acceptable in this class.

Preprocessing

In the preprocessing phase, a tool called the preprocessor searches your source code for lines starting with a #. These lines are known as preprocessor directives and tell the preprocessor to perform a certain action. The most common processor directive you will see is the #include preprocessor directive. This directive instructs the preprocessor to replace that line of code with the entire contents of another file (the one named within the < and >). The result of the preproccessor step is more C++ code. That modified C++ code serves as the input to the compilation step.

Compilation

In the compilation step, our code (after it has been preprocessed) is converted into code that the computer can execute natively (known as object code). Object code is stored in object files. Even though object code is in a language that the computer can execute, it is missing some very important supporting information. That missing information is itself code (known as runtime libraries) that the compiler can execute and provides operations for doing low-level operations like accessing/storing files, communicating over the Internet, etc.

Linking

In the linking step, the native code generated from our preprocessed source code is combined with runtime libraries. Runtime libraries are support libraries provided by a high-level programming language or the operating system that support platform-specific functions that vary from hardware-vendor to hardware-vendor or operating system to operating system. For example, accessing a hard drive is something that varies depending on the operating system and the hardware vendor so there are runtime libraries that make it easier. If your program uses that functionality, it will need to be linked to those libraries.

Painting the Compilation Canvas

Compilation (aka building) is a pipeline where the output of one phase becomes the input to the next phase. Here is a visual description of the compilation process: