C++ Build & Compilation
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 2 of 104.
C++ builds in three stages: Preprocessing, Compilation, and Linking.
1. Preprocessing — #include directives are resolved, copying header contents into source files
#include <iostream> // System header
#include "my_file.h" // Your own header2. Compilation — Each .cpp file is compiled independently into an object file (.o)
// math_utils.cpp compiles into math_utils.o
// main.cpp compiles into main.o3. Linking — Object files are combined into the final executable
// math_utils.o + main.o → program.exeThe header file declares what exists (prototypes). The .cpp file provides the actual implementation. The linker connects calls in main.cpp to implementations in other .cpp files.
Challenge
MediumThe header file math_utils.h declares two functions. Implement them in math_utils.cpp:
doubleValue(int x)— returnsx * 2square(int x)— returnsx * x
Make sure to include the header file in your .cpp file.
Cheat sheet
C++ builds in three stages: Preprocessing, Compilation, and Linking.
Preprocessing — #include directives are resolved, copying header contents into source files:
#include <iostream> // System header
#include "my_file.h" // Your own headerCompilation — Each .cpp file is compiled independently into an object file (.o):
// math_utils.cpp compiles into math_utils.o
// main.cpp compiles into main.oLinking — Object files are combined into the final executable:
// math_utils.o + main.o → program.exeThe header file declares what exists (prototypes). The .cpp file provides the actual implementation. The linker connects calls in main.cpp to implementations in other .cpp files.
Try it yourself
#include <iostream>
#include "math_utils.h"
int main() {
int num;
std::cin >> num;
std::cout << "Double: " << doubleValue(num) << std::endl;
std::cout << "Square: " << square(num) << std::endl;
std::cout << "Build successful" << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container