So far, every program in this chapter has come with its output already
printed. This section shows how to produce that output yourself: you type
a command, a compiler turns sum.cpp into an executable, and you run it.
Everything later in the course, from looking at machine code in Chapter 1
to building your first assembly program in Chapter 6, starts with these
same few commands.
The terminal
A terminal is a window where you control the computer by typing
instead of clicking. You type a line, press Enter, and the computer does
it and shows you any text that comes back. The program inside the window
that reads your lines is called the shell. Windows uses one called
PowerShell, macOS uses zsh, and most Linux computers use bash. When the
shell is ready for you, it shows a short marker called the prompt,
often ending in $. This course shows the prompt as $ in front of each
command. Don't type the $ itself.
Every line you type is cut into pieces at the spaces. The first piece is
the name of a program to start. Everything after it is extra information
for that program, called its arguments. In mkdir ch00, the program is
mkdir ("make directory", that is, make a folder) and its one argument
is ch00, the name of the new folder.
The terminal is always "standing in" one folder, called the working
directory. When you mention a file by its bare name, like sum.cpp,
the terminal looks for it in that folder. Four commands let you look
around and move:
pwd("print working directory") tells you which folder you're in.lslists what's in that folder. On Windows,dirdoes the same, and PowerShell also acceptsls.cd ch00("change directory") steps into the folderch00.cd ..steps back out to the folder that contains this one.mkdir ch00makes a new folder calledch00right where you are.
One more habit you'll use constantly. To start a program that's sitting
in the folder you're in, put ./ in front of its name on macOS and Linux
(./sum), or .\ on Windows (.\sum.exe). The dot means "this folder".
Without it, macOS and Linux only look for programs in a list of
standard folders, kept in a setting called PATH. They'll say "command
not found" even though your program is right there.
A terminal is a window in which you type commands and read their
text output. Inside it runs a shell, the program that reads each line
you type and carries it out: PowerShell on Windows, zsh on macOS, and
usually bash on Linux. The shell shows a prompt (often ending in $,
% or >) when it's ready for the next command. This course writes the
prompt as $; you don't type it.
Later in this section you'll type a compiler driver there. A compiler
driver is the one command that builds a program from its source.
A command line is split at spaces. The first piece names a program to
start, and the remaining pieces are its arguments, handed to that
program. mkdir ch00 starts mkdir with one argument, ch00.
The shell always has a working directory: the folder it's currently
in. A file name with no folder in front of it, like sum.cpp, means "the
file of that name in the working directory". Four commands cover what
this course needs:
| Command | What it does |
|---|---|
pwd |
print the working directory |
ls (or dir on Windows) |
list what's in it |
cd name |
move into the folder name; cd .. moves up one level |
mkdir name |
make a new folder name in the working directory |
To run a program that sits in the working directory, put the folder in
front of its name: ./sum on macOS and Linux, .\sum.exe on Windows.
. means "this folder". Without it, macOS and Linux shells search only the
folders named in a setting called PATH, and report "command not
found".
Open the Start menu and search for Developer PowerShell for VS. It
is installed with Visual Studio's Desktop development with C++ option,
and it already knows where Microsoft's compiler cl is. If you'd rather
use Clang, the same installer can add it (next part of this section).
mkdir asm-course
cd asm-course
pwd
Open Terminal (Applications › Utilities, or press ⌘-Space and type
Terminal). The shell is zsh, and your prompt usually ends in %.
mkdir asm-course
cd asm-course
pwd
Open your distribution's terminal (often Ctrl+Alt+T). The shell is usually bash.
mkdir asm-course
cd asm-course
pwd
Which job does each command do?
Sort each command by the job it does.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
pwd reports where you are; ls and dir list what is there; cd with a folder name or .. moves you; mkdir makes a folder and leaves you where you were; ./sum starts the program called sum that sits in the folder you are in.Worked example
Where am I, and where is my file?
-
1
Lena types
clang++ -o sum sum.cppand getsclang++: error: no such file or directory: 'sum.cpp'. She's sure she saved the file. -
2
A bare name like
sum.cppis looked up in the working directory, so the first question is where the terminal is standing.pwdprints/Users/lena: her home folder. -
3
lsshows a folder calledasm-coursebut nosum.cpp. She saved the file insideasm-course. -
4
cd asm-course, thenlsagain: nowsum.cppis listed. The same build command works. The file was never lost. The terminal was standing one folder too high.
Make a folder for this chapter
Your terminal starts in your home folder, which contains asm-course. Put the commands in order to make a folder called ch00 inside asm-course, step into it, and check where you ended up.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
asm-course before mkdir ch00, so the new folder lands there. cd ch00 steps into it, and pwd confirms the path ends in asm-course/ch00.Check your understanding (counts toward your progress)
You're in /home/ana/asm-course/ch00. What does cd .. do?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
One .. goes up one level only. You'd need it twice to reach /home/ana.
mkdir makes folders. cd moves you.
.. always means the folder that contains this one, not the one you were in before.
Why
.. is the folder containing the current one, so cd .. moves up one level, from ch00 to asm-course.
Two dots mean the folder one level up.
You're in asm-course/ch00. Order the commands to make a folder ch01 beside it (inside asm-course), step into it, and check where you are.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
cd .. moves up into asm-course, so mkdir ch01 makes the folder there. cd ch01 steps in, and pwd confirms the path ends in asm-course/ch01.
Correct answer
Also accepted:
Why
mkdir makes the folder wherever you're standing.
Which three-letter command prints the folder your terminal is standing in?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
pwd prints the working directory. It works in PowerShell, zsh and bash.
Correct answer
Also accepted:
Why
It stands for print working directory.
Sort each command: does it move you, make something, or just report?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
cd moves you. mkdir makes a folder and leaves you where you are. pwd, ls and dir only report.
Correct answer
Also accepted:
Why
Only one of these changes which folder you're in, with different arguments.
Practice: Moving around in the terminal (4 problems · doesn’t affect your progress)
This course writes commands as $ pwd. What do you type?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The $ is the prompt the shell shows. You don't type it.
The $ isn't part of the command at all.
Nothing follows the command here.
Why
$ marks the prompt, which the shell prints. You type only pwd.
The $ stands for the prompt.
pwd
What two characters mean the folder that contains the current one?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
.. is the folder one level up, so cd .. moves up.
Correct answer
Also accepted:
Why
cd uses them to go up one level.
On Windows, how do you run sum.exe, which sits in the folder PowerShell is in?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
sum.cpp is the source text, not the program.
cd moves into folders.
mkdir would try to make a folder.
Why
.\sum.exe says "sum.exe in this folder"; the dot means this folder.
Point at this folder, then name the program.
.\sum.exe
pwd prints /home/ana/asm-course/ch00. You type cd .. twice. Where are you now?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That's after one cd .. only.
That would take three.
The shell moves you; .. isn't kept as part of the path.
Why
The first cd .. goes to asm-course, the second to ana: /home/ana.
Each cd .. drops the last folder from the path.
/home/ana
The compiler driver
The compiler driver is the command you type to build your program.
There are three common ones: clang++, g++, and Microsoft's cl.
Turning your C++ file into an executable (the runnable file of machine
code from §1) actually takes several separate jobs, each done by a
different tool. You don't run them one by one. The driver is like a
foreman: you give it one order, and it gets each tool to do its job in
the right order, then hands you the finished executable. Chapter 1 opens
up those jobs one at a time.
The pieces you type after the driver's name are its arguments, and most
of them are options: settings that start with a dash (or, for cl, a
slash). Here's a full build command, piece by piece:
clang++ -std=c++20 -Wall -Wextra -o sum sum.cpp
clang++is the program to start, the driver.-std=c++20says which edition of C++'s rules to use; this course uses the 2020 edition.-Wall -Wextraask for warnings. A warning means "this is legal C++, but it's probably not what you meant". Treat each one as a bug to fix.-o sumnames the resultsum. Leave it out and you get a file calleda.out, which is easy to mix up.sum.cppis the file to read.
With Microsoft's cl, the same ideas are spelled with slashes:
cl /std:c++20 /W4 /EHsc sum.cpp builds sum.exe. (/EHsc switches on
C++'s standard exception-handling rules, and cl also leaves behind a file
called sum.obj, which Chapter 1 explains.)
Learn what success looks like: nothing. If the build works, you just get your prompt back, and a new file appears. If it fails, you get messages that name the file, the line and the column of each problem, and no new executable is written. If an old executable is still lying around from an earlier build, running it runs the old version, not your latest edit.
A compiler driver is the command you type to build a program:
clang++ (LLVM's Clang), g++ (GCC) or cl (Microsoft's compiler, part
of Visual Studio). Turning sum.cpp into an executable takes several
separate steps, each done by its own tool. The driver runs all of them
in order and hands each the right settings, so one command produces one
executable. Chapter 1 §2 stops the driver after each step so you can see
what each one makes.
Every argument after the driver's name is either an option (starting
with -, or / for cl) or an input file:
| clang++ / g++ | cl | Meaning |
|---|---|---|
-std=c++20 |
/std:c++20 |
which edition of the C++ rules to follow |
-Wall -Wextra |
/W4 |
report warnings: legal code that is probably a mistake |
-O1, -O2 |
/O2 |
optimise: make faster machine code (Chapter 18) |
-o sum |
/Fe:sum.exe |
name the executable (otherwise a.out, or sum.exe for cl) |
sum.cpp |
sum.cpp |
the source file to build |
cl also needs /EHsc, which switches on standard C++ exception
handling. It leaves a sum.obj file beside the executable (Chapter 1 §2
explains it). Note that MSVC's /O1 asks for small code, not fast
code.
A successful build prints nothing and leaves an executable. A failed
build prints messages of the form file:line:column: error: ... and
does not write a new executable. If an older one is still there from
an earlier build, running it runs the old program. A warning doesn't
stop the build, but in this course a warning is treated as something to
fix.
Install Visual Studio 2022 or later (the free Community edition) with
the Desktop development with C++ workload, then open Developer
PowerShell for VS and go to the folder holding sum.cpp:
cl /std:c++20 /W4 /EHsc sum.cpp
.\sum.exe
Prefer Clang? The same workload can add C++ Clang tools for Windows;
then clang++ -std=c++20 -Wall -Wextra -o sum.exe sum.cpp works in the
same window.
Install Apple's command-line tools once (a dialog asks you to confirm):
xcode-select --install
Then, in the folder holding sum.cpp:
clang++ -std=c++20 -Wall -Wextra -o sum sum.cpp
./sum
On a Mac, the command g++ also exists, but it's Clang under another
name.
Install GCC and Clang with your package manager (Debian and Ubuntu shown;
Fedora uses sudo dnf install gcc-c++ clang):
sudo apt install g++ clang
Then, in the folder holding sum.cpp:
g++ -std=c++20 -Wall -Wextra -o sum sum.cpp
./sum
clang++ takes exactly the same options.
Here is sum.cpp, the program the commands above build, and what it
prints when you run it:
#include <cstdint>
#include <iostream>
int main() {
std::int32_t total = 0;
for (std::int32_t i = 1; i <= 10; ++i) {
total += i;
}
std::cout << "Hello from C++!\n";
std::cout << "1 + 2 + ... + 10 = " << total << '\n';
}
Hello from C++!
1 + 2 + ... + 10 = 55
From source file to output
Click each stage to see the command typed there and what the terminal showed.
Loading interactive…
Worked example
Reading an error message
-
1
A student builds this file and sees the message below.
#include <iostream> int main() { std::cout << "Hi\n" }hi.cpp:4:24: error: expected ';' after expression 4 | std::cout << "Hi\n" | ^ | ; 1 error generated. -
2
Read the first line in pieces: file
hi.cpp, line 4, column 24, then the kind of problem (error) and what the compiler expected: a;. -
3
The lines underneath repeat line 4, put a
^under column 24 (just after the closing quote), and even show the missing;on the last line. -
4
Add the semicolon, build again, and the build prints nothing: success. Because the failed build wrote no executable, there was nothing new to run until it was fixed.
Worked example
I fixed it, but it still prints the old text
-
1
Omar changes
HellotoHowdyinsum.cpp, saves, types./sum, and still seesHello from C++!. -
2
./sumruns the executable, and the executable was made by the last build, before his edit. Saving changes only the source file. -
3
He runs the build command again (it prints nothing), then
./sum. Now it printsHowdy from C++!. -
4
The habit: edit, save, build, run, in that order, every time. In a terminal, the Up-arrow key brings back the previous command, so rebuilding costs two keystrokes.
🛠️ Build it yourself
Hello + sum
The sum.cpp program from this page as a ready-made project folder, with
a test that checks its output. Build it with the commands above, then
change it as the README suggests.
What's inside (7 files)
- hello-sum/.devcontainer/devcontainer.json
- hello-sum/.vscode/extensions.json
- hello-sum/.vscode/launch.json
- hello-sum/.vscode/tasks.json
- hello-sum/CMakeLists.txt
- hello-sum/README.md
- hello-sum/src/sum.cpp
Unzip it, then build and test with cmake -S . -B build, cmake --build build, ctest --test-dir build. The README has the steps for every OS and IDE. In VS Code, open the folder: the test task (Tasks: Run Test Task) and the debugger (F5) are already set up.
Check your understanding (counts toward your progress)
Sort each piece of g++ -std=c++20 -Wall -o calc calc.cpp.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
g++ is the compiler driver. -std=c++20, -Wall and -o calc are options; -o takes a value, the name calc. calc.cpp is the input file.
Correct answer
Also accepted:
Why
Options start with a dash.
On Linux you build with g++ -std=c++20 game.cpp, leaving out -o. What is the executable called?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
You only get that name by asking for it with -o game.
.exe names come from Windows tools like cl.
The default doesn't use the source file's name at all.
Why
Without -o, clang++ and g++ write a.out. That's why the course always names the result with -o.
It's an old, fixed default name.
What does -o sum do in clang++ -o sum sum.cpp?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That isn't what -o means. It names the output.
The compiler driver never opens an editor.
Building and running are separate commands; you type ./sum yourself.
Why
-o sum names the executable. Without it, clang++ and g++ on macOS and Linux call it a.out.
o for output.
Yesterday's build left an executable sum. Today you edit sum.cpp, the build fails with an error, and you type ./sum anyway. What runs?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
A failed build produces no new executable at all, not a partial one.
A failed build leaves any old executable where it was.
The compiler never skips a broken line. It refuses the whole build.
Why
A failed build doesn't write a new executable, so the old sum from yesterday is still there, and that's what runs.
Did the failed build write anything?
Practice: Building with a compiler driver (5 problems · doesn’t affect your progress)
What is a compiler driver?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The driver is a program you start from the terminal, not part of the chip.
It's a program, not a person.
That's the terminal. The driver is one of the programs you start there.
Why
clang++, g++ and cl are compiler drivers: one command that runs each build step in order and leaves one executable.
You give it one order and it gets each tool to do its job.
The build command (clang++, g++, cl) that runs every step
What is a warning from the compiler?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Errors stop the build. Warnings don't, which is why they're easy to ignore.
It's about your code, not the hardware.
The compiler never runs your program.
Why
A warning flags legal code that's likely wrong. The executable is still written, but this course treats every warning as something to fix.
-Wall asks for these.
Probably-wrong legal code; the build still finishes
Which two-character option tells clang++ or g++ what to name the executable?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
-o, followed by the name: -o sum.
Correct answer
Also accepted:
Why
o for output.
Which command builds hello.cpp into an executable called hello, with warnings on?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Without -o, the extra piece hello is taken as a second input file, which doesn't exist.
This names the output hello.cpp, after the source file, and asks to read an input called hello that doesn't exist.
That tries to run a program that hasn't been built yet.
Why
-o hello names the result, and hello.cpp is the input. The options before them pick C++20 and turn on warnings.
-o must come right before the name you want.
clang++ -std=c++20 -Wall -Wextra -o hello hello.cpp
Building an executable takes several separate tools. Why do you type only one command?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Every step is needed to get an executable. The driver just hides them.
The terminal only starts the one program you name.
The processor runs finished programs; it doesn't build them.
Why
That's the driver's whole job: you give one order and it runs each tool in turn. Chapter 1 §2 runs them one at a time.
Think of a foreman handing out jobs.
The compiler driver runs each tool in order with the right settings
Practice: Who does which job in a build (4 problems · doesn’t affect your progress)
Which of these is a compiler driver?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
pwd is a terminal command that reports which folder you are in. It builds nothing.
sum.cpp is the source file, the input a driver reads.
./sum runs a program that has already been built.
Why
g++ is GCC's compiler driver, like clang++ and Microsoft's cl. The others are a terminal command, a source file and a request to run a finished program.
Look for the command whose job is building.
g++
In cl /std:c++20 /W4 /EHsc sum.cpp, what is cl?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That option is /W4. Options for cl start with a slash.
The source file is sum.cpp, at the end of the line.
cl names its executable after the source file, so this build makes sum.exe.
Why
The first piece is always the program the terminal starts. Here that is cl, the driver that comes with Visual Studio; everything after it is an option or an input file.
The first piece of a command line names the program to start.
Microsoft's compiler driver, the program this command starts
What is the terminal's part in a build?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Translating is the compiler's work, which the driver starts.
Choosing and ordering the steps is exactly what the driver is for.
Warnings come from the compiler, when you ask for them with -Wall or /W4.
Why
The terminal is where you type commands. It starts the one program you name, here the driver, and shows its messages; the driver and the tools it runs do the building.
The terminal would do the same for any program, not just a compiler.
It starts the program you name and shows its output
After a build that printed nothing, you type ./sum and see Hello from C++!. Which program printed that line?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The driver's work ended when the build finished. Running is a separate command.
The terminal only starts the program you name. Here that is sum, not sum.cpp.
A compiler finishes its translation before the program runs. Reading and running line by line is what an interpreter does.
Why
The build turned sum.cpp into the executable sum. Typing ./sum starts that executable, and its machine code prints the line; no compiler is involved any more.
Which file did ./sum name?
The executable sum, made by the build
Where this goes next
Chapter 1 §2 uses this same driver with extra options (-E, -S, -c)
to stop after each build step and show its output. Chapter 6 installs
the assembly tools for your system and builds programs that mix
C++ and assembly. Chapter 18 compares the machine code that -O0, -O1
and -O2 produce from the same function.