C++ Compiling
The formatting page left tidy braces and indent on left, right, and total, and the printed lines left=10, right=2, and 12 from box.cpp. This page stays in the Quality and Wrap-up module on purpose. The workshop file on disk stays box.cpp so you keep one familiar source path. The new work names compiling: the build step that turns that source into a runnable program. Keep the same names and the same plus. Run g++ -std=c++17 box.cpp -o box, then run ./box. The screen still shows left=10, then right=2, then 12. Random waits for the next page.
Compiling here means reading the source file and writing an executable you can run. Reading g++ is reading the compiler tool. Reading -std=c++17 is reading the language mode this course uses. Reading box.cpp is reading the input source. Reading -o box is reading the output name. Reading ./box is reading the run step after a successful build. Keep #include <iostream> so the prints work. Do not invent a random tool on this page.
Programs need this step every time the source changes before you trust a new run. You do not need a new workshop file name. The build command stays familiar: one source file box.cpp, one output name box, one run that prints three lines. You compile only box.cpp again for this workshop file on disk.
Think of compiling as a stamp press that turns a written plan into a machine you can start. The previous page cleaned the layout. This page names the press itself. Do not expect random in the checked file. The printed left=10, right=2, and 12 after a fresh build is the whole lesson point.
Above main, keep only the include. Keep int left = 10;, int right = 2;, the labeled prints, int total = left + right;, the total print, and return 0; with tidy braces. Keep #include <iostream> at the top. After the file is saved, build with g++ -std=c++17 box.cpp -o box. Then run ./box. Do not invent a random demo on this page.
Walk the path carefully from save to screen. You save box.cpp. The compiler reads that file. A successful build writes the executable named box. Running ./box starts main. It stores ten in left and two in right. The first print shows left=10. The second print shows right=2. The total uses plus, so twelve is stored. The third print shows 12. Then main returns and the program ends. There are three printed lines on the screen: left=10, then right=2, then 12. The checked file never skips the build step in the story.
Say the steps out loud once while you read the file. Save box.cpp. Compile with g++ to box. Run ./box. Read left=10, right=2, and 12. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No random keyword. No new math. When the screen shows left=10, right=2, and 12 after a fresh build, the story matched the source.
If you change the source but forget to compile again, an old box may still print yesterday's result. That failed experiment stays in the prose. The checked path always rebuilds before it trusts the screen.
If the build fails about a missing semicolon, fix the source first. Compiling does not invent a program from broken text. The checked file keeps a clean body so the build succeeds.
A common mistake is to treat ./box as the compile command. Another common mistake is to invent the word random as a required keyword here. A third mistake is to omit -o box and wonder where the output went. A fourth mistake is to wait for cin. A fifth mistake is to change the numbers while practicing the build line.
This page stops before the next tool, where a program picks a value that is not fixed in the source. The next page names that tool once at the end. The compiling habit you have just written only shows the build and run path on the same sum. Keep the printed 12 so the order stays clear for a first careful reading of the file.
Remember that compiling prepares the run; it does not replace plus. You still need a correct total to finish. After a successful build and run the screen shows left=10, right=2, and 12. After you undo a bad experiment the checked file returns to that build path. Stability of the checked file matters as much as learning the compile spelling itself.
Open box.cpp. Keep left, right, and total with tidy layout. Save the file. Build with g++ -std=c++17 box.cpp -o box. Run ./box. Do not add a random lecture inside the file. The program file name on disk stays box.cpp. The checked file is below, and the new habit is compiling with a clear build line.
#include <iostream>
int main()
{
int left = 10;
int right = 2;
std::cout << "left=" << left << std::endl;
std::cout << "right=" << right << std::endl;
int total = left + right;
std::cout << total << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows left=10, then right=2, then 12.
Check the three printed lines first. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the build fails, read the compiler message and fix the source. If the last line is 8, total still uses minus. If you ran an old executable, rebuild before you trust the compiling path.
📊 compiling build and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | left=10, because the first labeled print runs after ./box |
| Last | 12, because total still uses plus |
| Rule | g++ turns box.cpp into box; ./box runs the program |
| Screen | left=10 then right=2 then 12 |
Try one change at a time. Temporarily change total to print ready, rebuild with the same g++ line, run ./box, see ready, then restore the total print and rebuild again. Next, temporarily omit -o box only in a practice attempt you undo, then return to the checked command with -o box. Each experiment changes one thing on purpose, so you see either a different third line or a reminder that the output name matters. Undo each change before the next one starts, so the file and the build line always return to the checked workshop version with 12 once again after every single careful try back on disk again.
This page ends the compiling tool and stops before the next one. The next page is random, which names a value that is not fixed as 10 and 2 in the source. The compiling habit you have just written only shows g++ -std=c++17 box.cpp -o box and ./box on left and right from box.cpp while total still prints 12. You leave with left=10, right=2, and 12 on the screen. Count the printed lines before you leave. There are exactly three printed lines on that final screen, and those lines are left=10 then right=2 then 12 alone right there now after the final successful build run on disk today now after the compile check.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top