1. Goals
After this lesson, you can turn a C++ file into a runnable program, explain each line of a small example, and decide where to start fixing an error. You do not need to memorize every diagnostic: identify which step failed first.
2. Prerequisites
Know how to create and save a text file in a folder.
Know how to open your editor's terminal or Windows PowerShell.
Have GCC or Clang installed together with its C++ library.
If you do not have a compiler, Windows users can follow the official MSYS2 GCC installation guide. On Linux, use your distribution's GCC or Clang package; Clang's getting started page links to official releases. You do not need to build the compiler itself from source to study this lesson.
3. Explanation
The file main.cpp contains the instructions you write. The compiler checks
and translates your code; the linking step combines the translated code with
the library parts it needs. If these steps succeed, you get a program to run.
The g++ command below performs compilation and linking in one invocation.
For this small program, read the code from top to bottom:
#include <iostream>provides declarations for output throughstd::cout.int main()opens the part of the program you will write;mainis this example's starting point.The braces
{and}surround the statements that belong tomain.std::cout << ...sends content to the screen; you can chain several<<operations."Foundation ready"is text enclosed in double quotes.'\n'is a newline character, not two letters to print.A semicolon
;ends a statement;return 0;endsmainand reports successful completion.
The prefix std:: tells you that cout belongs to the standard library.
Keep that prefix; creating your own namespaces can wait.
The expression 2 + 3 adds two numbers, and its parentheses help separate
the calculation from the surrounding output operations.
Foundation uses C++17 mode as a common baseline for introductory examples. You are learning fundamentals that apply across language versions, rather than following the syntax restrictions of an old version.
The options -Wall -Wextra -Wpedantic enable useful groups of warnings;
they do not guarantee that every error will be found.
The GCC manual
explains that g++ adds the C++ library when linking, so use g++ below.
4. Runnable example
Save this exact content as main.cpp:
#include <iostream>
int main() {
std::cout << "Foundation ready" << '\n';
std::cout << "2 + 3 = " << (2 + 3) << '\n';
return 0;
}Open a terminal in the folder containing the file. In Windows PowerShell:
g++ -std=c++17 -Wall -Wextra -Wpedantic main.cpp -o foundation.exeOnly after that command succeeds, run:
.\foundation.exeIf PowerShell cannot find g++, check your compiler installation instructions.
For MSYS2 in its default location, add C:\msys64\ucrt64\bin
to your user Path environment variable, then reopen PowerShell.
This folder contains the compiler and the libraries needed to run the program.
On Linux, use these two commands, running the second only after the first succeeds:
g++ -std=c++17 -Wall -Wextra -Wpedantic main.cpp -o foundation
./foundationIf your machine is fully configured for Clang, replace g++ with clang++.
The option -std=c++17 selects the language version; -o names the output file.
The result is the same on both operating systems:
Foundation ready
2 + 3 = 5After editing the source, save it, rebuild the program, then run it again. Running an old executable does not automatically apply your source changes.
5. Common mistakes
Compilation error: omit a semicolon after a
std::coutstatement or misspellstd::cout. There is no new program to run. Read the first error and inspect the preceding line too.Linking error: code was translated, but a definition needed for the final program is missing. For example, a missing
maincan produce anundefined referenceorunresolved externalmessage.Problem during execution: the program starts but cannot complete an operation, such as opening a file that does not exist. The program needs to detect and handle it.
Logic error: the program finishes but gives the wrong answer, such as using
2 - 3when you intended to calculate a sum. The compiler cannot know your intention.
A diagnostic often contains a filename, line number, column number, and explanation.
For example, main.cpp:5:... directs you to line 5. The reported location can be
where the compiler notices a problem rather than where you made the mistake.
Fix one error and compile again before working through later messages.
6. Practice
Replace the greeting with
Hello, C++and check that the rebuilt program prints it.Add a line that prints
4 * 6 = 24, using4 * 6to calculate the result.Remove one semicolon, compile, and record the first error. Restore it and run again.
Change addition to subtraction while keeping the label
2 + 3 =. Does the program compile? Compare the result with the requirement to find the logic error.
You are ready to continue when you can fix the missing semicolon yourself and avoid accidentally running an old executable after a failed build.
7. Self-check
Why might running the program after editing
main.cppstill show the old result? Answer: the executable changes only after successful compilation and linking. Save the source, rebuild, and run the newly created file.Does
std::cout << "2 + 3";print the number5? Answer: no; double quotes make it text, so it prints2 + 3. To calculate the sum, writestd::cout << (2 + 3);.Does finishing without an error prove that the result is correct? Answer: no. Logic errors can remain; compare against expected results and try other inputs.
return 0;is merely the success status the program reports.
Sample code
Source file
cpp98_foundation/01_build_run_errors/main.cpp
#include <iostream>
int main() {
std::cout << "Foundation ready" << '\n';
std::cout << "2 + 3 = " << (2 + 3) << '\n';
return 0;
}