Table of Contents |
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.
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]
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.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.
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.
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.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.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:
EXAMPLE
Once all of the car’s components have been assembled, the result is a finished vehicle that can be driven.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.
Source: THIS TUTORIAL WAS AUTHORED BY SOPHIA LEARNING. PLEASE SEE OUR TERMS OF USE.