What C++ is: compile and run
C++ is a compiled language: before a program can run, the text you write is turned into machine code your CPU understands. Follow a five-line hello world as one command sends it through the preprocessor, the compiler and the linker into an executable file, then run it, and see the difference between a compile error and a run-time error.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Which tool turns C++ source code into machine code?
The compiler checks the code against the rules of C++ and translates it into machine code, saved in an object file such as hello.o. The preprocessor only handles the # lines, and the linker joins finished pieces together.
02What does #include <iostream> do?
Lines starting with # are for the preprocessor. #include pastes in another file's text before the compiler sees the code.
03Where does a C++ program start running?
Every C++ program has exactly one main function. Running the program means running main, from its first line; return 0 ends it and reports success.
04You forget a ; and build the program. What happens?
A missing ; breaks the rules of C++, so the compiler stops before anything can run. You fix the source and build again.
05Why is a compiled C++ program usually fast?
You compile once; the executable is already machine code that the CPU runs directly. C++ also lets you control where data lives in memory and for how long.
Glossary
- Source code
- The program as text you write and can read, saved in a file such as hello.cpp.
- Compiled language
- A language whose programs are translated into machine code before they run. C++ is one.
- Preprocessor
- The first tool in a C++ build. It follows the lines that start with #, for example pasting in a header with #include.
- Compiler
- The tool that checks code against the rules of the language and translates it into machine code.
- Object file
- The compiler's output for one source file (hello.o, or hello.obj on Windows): machine code that is not yet a whole program.
- Linker
- The tool that joins object files and library code into one executable file.
- Executable
- A file of machine code the operating system can load and run, such as hello (hello.exe on Windows).
- main
- The function where every C++ program starts. Its return value tells the system how the program ended; 0 means success.
Notes and sources
- The C++ standard describes nine phases of translation; the figure groups them into the three tools you meet in practice. One command such as g++ hello.cpp -o hello runs all three for you.
- File names differ between systems: object files end in .o on Linux and macOS and .obj on Windows, and the executable is hello.exe on Windows. On Windows with Microsoft's compiler the command is cl /EHsc hello.cpp.
- Exact error messages differ between compilers. GCC and Clang both report the missing semicolon as "expected ';'", with the file name and line number.
- Most of the standard library's code is linked in automatically from the library that ships with your compiler (libstdc++ with GCC, libc++ with Clang, the MSVC STL on Windows). Some of it is pasted in by #include and compiled with your code.
- Where C++ is used, including games, web browsers and databases: Bjarne Stroustrup's list of C++ applications (stroustrup.com/applications.html) and the isocpp.org Big Picture FAQ.
- The program on the card is real, standard C++. Build it with g++ hello.cpp -o hello (or clang++), run ./hello and it prints Hello!
- cppreference.com: Phases of translation
- cppreference.com: Source file inclusion (#include)
- cppreference.com: Main function
- cppreference.com: std::cout
- Standard C++ Foundation (isocpp.org): Get Started!
- GCC manual: Options Controlling the Kind of Output (-E, -c, -o)
- Microsoft Learn: Walkthrough: Compiling a Native C++ Program on the Command Line
- Standard C++ Foundation (isocpp.org): Big Picture Issues FAQ
- Bjarne Stroustrup: C++ Applications
Quiz Unlocks after the last step.