Building and Running a C++ Program
A C++ program becomes runnable through preprocessing, compilation, assembly, and linking. Preprocessing handles #include, #define, and conditional compilation.
Summary
A C++ program becomes runnable through preprocessing, compilation, assembly, and linking. Preprocessing handles #include, #define, and conditional compilation. A translation unit is a source file after preprocessing, including content introduced through headers.
| Stage | Main task | Output |
|---|---|---|
| Compilation | Translates a preprocessed translation unit | Assembly code |
| Assembly | Converts assembly into machine code and metadata | Object file |
| Linking | Combines object files and libraries and resolves external symbols | Executable |
Object files commonly use .o or .obj extensions. Separate compilation allows existing object files to be reused when their source files have not changed, reducing rebuild time.
With GCC, g++ main.cpp -o app builds an executable, while g++ -c main.cpp produces an object file without linking. On a typical Unix-like system, ./app launches the executable from the current directory. Hosted C++ execution enters main after runtime initialization. Returning zero from main reports successful termination to the execution environment through an exit status.
Syntax and type errors arise during compilation; an undefined reference is a linker error indicating that a referenced symbol lacks a required definition in the linked inputs. The command g++ -g -O0 main.cpp -o app includes debugging information and disables optimization for easier source-level inspection. Breakpoints pause execution so program state can be inspected.
Executables target particular platforms and architectures. Distributing source still requires compatible compilers and libraries.
Common Misconceptions: A successful build does not guarantee correct behavior; logical errors and undefined behavior may remain. An object file is not the final linked executable.
🧠 Key Concepts
- Preprocessing
- Translation units
- Compilation
- Assembly
- Object files
- Symbol resolution
- Separate compilation
- Breakpoints
- Exit status
🧠 Quick Check
See what you remember from the summary.
Which build stage processes #include, #define, and conditional compilation directives?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Building and Running a C++ Program
📘 Overview A C++ program becomes runnable through preprocessing, compilation, assembly, and linking. Separating these build stages helps distinguish source-code errors, unresolved dependencies, and failures that occur during execution.
🧠 Key Idea Building translates source files into object files and links them with required libraries to produce an executable. Running executes that artifact; a successful build does not guarantee correct behavior.
⚔️ Core Details: - Preprocessing handles directives such as #include, #define, and conditional compilation before translation into machine code. - Compilation translates each preprocessed translation unit into assembly; assembly converts that output into an object file. - Linking combines object files and required libraries, resolves external symbol references, and produces an executable. - With GCC, g++ main.cpp -o app builds an executable; g++ -c main.cpp produces an object file without linking. - On a typical Unix-like system, ./app launches the executable from the current directory; hosted C++ execution enters main after runtime initialization. - A debug build such as g++ -g -O0 main.cpp -o app includes debugging information and disables optimization for easier source-level inspection.
🎯 Why It Matters: - Identifying the failing build stage narrows troubleshooting: syntax and type errors differ from linker errors such as undefined references. - Separate compilation allows unchanged source files to reuse existing object files, reducing rebuild time in larger projects. - Testing and debugging remain necessary because logical errors and undefined behavior can exist in successfully built programs. - Executables target a particular platform and architecture; distributing source code does not eliminate compiler or library compatibility requirements.
🧠 Quick Recall: - Translation unit - A source file after preprocessing, including the content introduced through its headers. - Object file - Machine code and metadata awaiting linking; commonly stored with a .o or .obj extension. - Undefined reference - A linker error indicating that a referenced symbol lacks a required definition in the linked inputs. - Breakpoint - A debugger stop point for inspecting program state and stepping through execution. - Exit status - A value reported to the execution environment; returning zero from main indicates successful termination.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.