Your First C++ Program: Compiling, Linking and the Standard Version
Write, compile and run a first C++ program, see what the preprocessor, compiler and linker each do with real measurements, and check the language standard that the compiler used.
In this lesson you will
- Write, compile and run a C++ program
- Explain preprocessing, compiling and linking
- Read the main function
- Inspect real build products
About 28 minutes
Elena wants to log her weather station
Elena has a small weather station on her roof and wants a program that reads its numbers, computes statistics and keeps a log. She has chosen C++ because it is fast, it gives her direct control over memory, and it is used in games, embedded devices and many systems. C++ is a compiled language: you write source code, a program called a compiler translates it into machine code for your processor, and you run the result directly, with no virtual machine in between. That gives speed and control, and it also means that you are responsible for some things that other languages do for you.
From source to running program
Building a C++ program takes three steps. The preprocessor handles lines that begin with a hash sign, such as #include, which pastes the contents of another file into yours. The compiler turns each source file into an object file of machine code. The linker joins the object files and the libraries into one executable. The command g++ runs all three, and flags choose the options: -std=c++20 selects the language standard, -Wall and -Wextra turn on helpful warnings, and -o names the output file.
| Step | Tool | Result |
|---|---|---|
| Preprocess | The preprocessor inside g++ | Your code with all #include files pasted in |
| Compile | g++ -c | An object file (.o) of machine code |
| Link | g++ (calls the linker) | The executable |
Execution starts in a function called main, which returns an int: zero means success. Output goes through std::cout, from the header , and the << operator sends values to it. Names from the standard library live in the namespace std, which is why they begin with std::. The program below prints a greeting, a reading, and the value of the macro __cplusplus, which the compiler sets to the number of the standard that it is using.
#include <iostream>
int main() {
double temperature = 21.5;
std::cout << "Elena's weather station\n";
std::cout << "Temperature: " << temperature << " C\n";
std::cout << "Language standard macro: " << __cplusplus << "\n";
return 0;
}
Elena's weather station
Temperature: 21.5 C
Language standard macro: 202002
The program printed the temperature, 21.5, and the standard macro 202002, which is the value that C++20 defines, so the compiler really was using that standard. Notice that the program ends with return 0, and that “\n” starts a new line.
To see the three steps, the course ran them separately on the same source. The source is only 9 lines, but after preprocessing it is 43,175 lines, because #include pasted in the whole text of the header and everything that it includes. This is why large C++ projects take time to compile, and why headers are kept as small as possible. The compile step produced an object file of 2,488 bytes, and the link step an executable of 16,440 bytes, which is bigger because it holds the code from the object file plus start-up code and the references to the standard library. The first four bytes of the executable are 7F followed by the letters E, L and F, the signature of the executable format used on Linux. On Windows an executable starts with the letters MZ instead, and the same C++ source builds on both.
Things that trip beginners
- A missing semicolon after a statement or after a class definition produces confusing errors, so read the first message first.
- Names are case sensitive.
- Warnings matter: -Wall -Wextra find many real bugs, so read them and fix them.
- The file extension is usually .cpp, and headers use .h or .hpp.
- Do not write using namespace std; in headers. It pollutes every file that includes them.
Why C++ asks more of you
In many languages a runtime system takes care of memory, checks every array access and catches many mistakes while the program runs. C++ leaves most of that to you, in return for speed and control, so the compiler’s warnings, and later the sanitizers that this course uses, are your safety net. The habit that serves you best from the first day is to compile with warnings enabled, to treat every warning as something to understand, and to keep programs small enough that you can reason about every line. Later lessons show the tools that make this practical: standard containers instead of raw arrays, smart pointers instead of manual memory, and tests that run on every change.
Use at least -Wall -Wextra. Many serious bugs, such as unused variables, missing returns and comparisons between signed and unsigned numbers, are reported as warnings and not as errors.
One mistake can create dozens of messages. Fix the first, rebuild, and see how many are left.
Every program in this lesson was compiled with GCC 13 using -std=c++20 -Wall -Wextra and run while the course was written. C++ has a new standard every three years and compilers differ in what they support, so check your compiler’s documentation and cppreference.com for the current state.
Practice
Compile the program with g++ -std=c++20 -Wall -Wextra and run it. Change the temperature, add a line with the humidity, and build again. Then remove a semicolon on purpose and read the compiler message carefully, noting the file, the line and the column that it reports.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does the preprocessor do with #include?
- Compiles the file
- Pastes the contents of another file
- Runs the program
- Deletes the line
That is why preprocessed code is so long.
What does the linker do?
- Joins object files and libraries into an executable
- Writes source code
- Formats code
- Checks spelling
The last step of the build.
What does main return, and what does zero mean?
- An int, and zero means success
- A string, and zero means failure
- Nothing
- A float
The operating system reads it.
What did the __cplusplus value tell us?
- The program size
- The number of lines
- The operating system
- The language standard in use
202002 is C++20.
Why are the warning flags recommended?
- They make code faster
- They are required
- They shorten code
- They find many real bugs
-Wall -Wextra.
Summary
- C++ is compiled to machine code: preprocess, compile, link.
- Execution starts in main, which returns an int.
- -std=c++20 -Wall -Wextra is a good set of flags.
- Real build products can be inspected: line counts, object files and the ELF header.
Questions
Which compiler should I use?
GCC, Clang and Microsoft Visual C++ are the main ones. The language is the same, and support for new features differs, so check each compiler's documentation.
What is the difference between C and C++?
C++ grew out of C and adds classes, templates, the standard library containers and much more. A separate course covers C.
Is C++ hard to learn?
It has a lot in it. Learning it in small steps with the compiler's warnings turned on makes it manageable, and this course follows that approach.
