Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

Compilers and the Build Process

Author: Sophia

what's covered
Before a computer can run a C++ program, the source code written by a programmer must go through several stages that prepare it for execution. This lesson explores how source code becomes a runnable program by introducing compilation, the role of the compiler, and the overall build process. You will examine how code is checked for errors, translated into machine-readable instructions, and combined into an executable program that the operating system can run. Understanding this process will help you make sense of how C++ programs are created and executed. Specifically, this lesson will cover the following:

Table of Contents

1. From Code to Program

When you write a C++ program, those instructions are written in a form that people can read and understand. However, the computer cannot run that code immediately. Before the program can run, it must pass through several stages that prepare it for execution.

This sequence of stages is called the build process. During the build process, your code is checked for errors, translated into machine code, combined with any additional code it needs, and turned into a program that the operating system can run.

The overall build process is shown below. Do not worry if some of the terms are unfamiliar right now. You will learn what each stage does throughout this lesson.

[PLACEHOLDER]

Each stage produces something the next stage needs. If a problem occurs during any stage, the process stops until the issue is corrected.

recall
As you learned previously, source code is the human-readable code written by a programmer. Before that code can run, it must pass through several stages that prepare it for execution.

Source code can also contain comments, which are notes written for people reading the code. Programmers use comments to explain how code works, document important information, or leave reminders for themselves and other developers.

Unlike program instructions, comments are not intended to be executed by the computer. During compilation, the compiler ignores comments completely and does not include them in the final program. Comments exist only to help people understand the source code.

Computers, however, do not understand C++ directly. A computer can only execute instructions written in machine code, a low-level form of code represented as patterns of 0s and 1s.

Writing programs directly in machine code would be difficult and time-consuming. Instead, programmers write source code and use software tools to convert it into a form the computer can execute.

The process of converting source code into machine code is called compilation. During this process, the compiler takes the human-readable code written by a programmer and translates it into instructions the computer can execute.

Because C++ programs must go through compilation before they can run, C++ is known as a compiled language.

The following image illustrates the translation from source code to machine code.

[PLACEHOLDER]

big idea
Programmers write source code, but computers execute machine code. The build process bridges the gap between the two by transforming human-readable code into a runnable program.

terms to know
Program
A set of instructions written in a programming language that tells a computer what to do.
Build Process
The sequence of steps that transforms source code into an executable program that a computer can run.
Comment
A note written in source code to help people understand a program that is ignored during compilation and does not affect program execution.
Machine Code
Instructions written in 0s and 1s that a computer can run directly.
Compilation
The process of translating source code into machine code so a computer can execute it.


2. What a Compiler Does

One of the most important tools in the build process is the compiler. The compiler is responsible for checking your source code and translating it into machine code that a computer can understand.

[PLACEHOLDER]

The compiler is the first major stage of the build process. Before your program can become a runnable application, the compiler must verify that your code follows the rules of C++ and then convert it into machine code.

If the compiler finds a problem, it stops the build process and reports an error. These messages help programmers identify and fix issues before the program runs.

EXAMPLE

Think about using a spell checker when writing a paper. If the spell checker finds a mistake, it points it out so you can correct it before submitting your work.

A compiler works in a similar way. It checks source code for problems and reports them so the programmer can fix them before the program is built.

Compiler errors are useful because they help identify problems before anyone uses the program. Instead of failing while the program is running, many mistakes can be found and corrected during the build process.

key concept
The compiler checks source code for errors and translates it into machine code. When compilation is successful, the compiler produces an object file that moves to the next stage of the build process.

terms to know
Compiler
A program that checks source code for errors and translates it into machine code.
Compiler Error
A message produced by the compiler when it detects a problem in the source code that prevents successful translation.


3. The Build Process

Compilation is only one part of transforming source code into a runnable program. After source code is compiled, additional steps must occur before the program can run. Together, these stages form the build process.

After compilation produces an object file, the linker combines the required pieces and creates an executable file that the operating system can run.

3a. Object File

When compilation finishes successfully, the compiler produces an object file. An object file contains machine code generated from the source code, but it is not yet a complete program.

An object file serves as an intermediate step in the build process. It contains the translated instructions from the source code, but additional code may still need to be connected before the program can run.

EXAMPLE

Think of building a car. Different teams create different components, such as the engine, wheels, and doors. Each component is completed separately, but none of them can function as a car on its own.

An object file works in a similar way. It contains one completed piece of a program, but additional pieces must still be combined before the program can run.

term to know
Object File
A compiled file containing machine code translated from a single source file, which must be linked with other code to form a complete program.

3b. Linker

After the object file is created, the next stage involves a tool called the linker.

The linker combines the object file with any additional code the program requires. Programs often rely on code that programmers did not write themselves. For example, a program that displays text on the screen uses functionality provided by the C++ standard library.

The linker connects all of the required pieces together to create a complete program.

If the linker cannot find code that the program depends on, the build process stops and reports a linker error.

EXAMPLE

As the car continues through production, the individual components must be assembled into a single vehicle. The engine, wheels, doors, and other parts are connected so they can work together.

The linker performs a similar role during the build process. It combines the compiled pieces of a program into a complete application.

key concept
The compiler creates an object file. The linker combines that object file with any additional code required to create a complete program.

term to know
Linker
A program that takes object files from the compiler and combines them into a single unit, connecting any prebuilt code the program needs.

3c. Executable File

Once the linker finishes combining all of the required pieces, the result is an executable file.

An executable file contains the instructions needed for the operating system to load and run a program. When you open an application on your computer, you are running an executable file.

The build process transforms source code into an executable file through a sequence of stages:

  1. Source code is compiled into an object file.
  2. The linker combines the object file with any required code.
  3. An executable file is created.
After these stages are complete, the operating system can run the program.

EXAMPLE

Once all of the car’s components have been assembled, the result is a finished vehicle that can be driven.

Similarly, after all stages of the build process are complete, the result is an executable file. The operating system can load and run this finished program.

IN CONTEXT

Many software applications contain thousands or even millions of lines of code spread across many files. Developers rarely build these programs by hand. Instead, automated tools run the build process whenever changes are made.

For example, a video game may contain code written by teams responsible for graphics, sound, networking, and gameplay. Each team’s code is compiled separately, linked together, and transformed into an executable file before the game can be tested or released.

Although the programs you create in this course will be much smaller, they go through the same build process used by professional software developers.

try it
A program compiles successfully, but no executable file is created. Use what you learned about the build process to answer the following questions.
Which stage of the build process may have encountered a problem?
The linker may have encountered a problem while combining the object file with other required code.
Why would a problem at this stage prevent the executable file from being created?
The executable file cannot be created until the linker successfully combines all of the required pieces of the program. If linking fails, the build process cannot continue.
What does this situation tell you about the relationship between the linker and the executable file?
The linker must complete its work before an executable file can be created. The executable file is the final result of the build process after all required code has been successfully combined.

term to know
Executable File
The final output of the build process: a file containing complete, ready-to-run instructions that the operating system can load and execute directly.

summary
In this lesson, you learned how source code is transformed into a runnable program. Looking at the transition from code to program, you explored the relationship between source code, machine code, comments, compilation, and the build process. When exploring how a compiler functions, you learned how the compiler checks source code for errors and translates it into machine code. Overall, the build process showed you how an object file is created, how a linker combines required pieces of code, and how the process produces an executable file. Together, these stages transform human-readable source code into a program that the operating system can run.

Source: THIS TUTORIAL WAS AUTHORED BY SOPHIA LEARNING. PLEASE SEE OUR TERMS OF USE.

Terms to Know
Build Process

The sequence of steps that transforms source code into an executable program that a computer can run.

Comment

A note written in source code to help people understand a program that is ignored during compilation and does not affect program execution.

Compilation

The process of translating source code into machine code so a computer can execute it.

Compiler

A program that checks source code for errors and translates it into machine code.

Compiler Error

A message produced by the compiler when it detects a problem in the source code that prevents successful translation.

Executable File

The final output of the build process: a file containing complete, ready-to-run instructions that the operating system can load and execute directly.

Linker

A program that takes object files from the compiler and combines them into a single unit, connecting any prebuilt code the program needs.

Machine Code

Instructions written in 0s and 1s that a computer can run directly.

Object File

A compiled file containing machine code translated from a single source file, which must be linked with other code to form a complete program.

Program

A set of instructions written in a programming language that tells a computer what to do.