Master C++

Your first program

2 / 235
Contents

Contents

Master C++

0 of 235 complete

6 min read

The tools between your file and a running program

Two tools, four stages, and the names for each of them, because those names are what the messages use.

Saying that the compiler turns your text into machine code is true as far as it goes, and it leaves a step out. Two separate tools do the work, and the stages in between have names of their own. Those names are worth a few minutes now, because they are the words the messages use when something goes wrong.

The end of the whole business is an executable: a file of machine code that the operating system can start on its own. Once it exists the tools that made it are out of the picture. You could delete the compiler from the machine and the executable would still run.

One more tool has a name worth knowing. Your program will use code somebody else wrote, such as the instructions for getting text onto a screen. That code lives in the standard library, a large collection that comes with every C++ compiler. The linker is the tool that joins your translated code to the pieces of the library it used, and produces the finished executable.

From text you typed to a program that runsFour stages. Only the last one is your program actually doing anything.

Step 1 of 4. 1. You write it

greet.cpp
--------------------------------
#include <iostream>
int main() { ... }
--------------------------------

Ordinary text. You could open it in any text editor, or print it out.

Two phrases now get used constantly, so pin them down here. Compile time is while the compiler is reading your source code. Run time is afterwards, while the finished program is doing its job. Almost every message in the lesson on errors, two along from this one, arrives at compile time, before anything has run at all.

You add a line to a 500 line program and accidentally spell std::cout as std::cuot. When do you find out?

At compile time, before anything runs, and regardless of whether that line would ever have been reached. There is no name cuot in the standard library, so the compiler refuses to translate the file and produces no executable. Nothing anybody runs will ever contain that mistake.

All lessons in Master C++