Menu
Coddy logo textTech

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 header

2. 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.o

3. Linking — Object files are combined into the final executable

// math_utils.o + main.o → program.exe

The 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 icon

Challenge

Medium

The header file math_utils.h declares two functions. Implement them in math_utils.cpp:

  • doubleValue(int x) — returns x * 2
  • square(int x) — returns x * 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 header

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.o

Linking — Object files are combined into the final executable:

// math_utils.o + main.o → program.exe

The 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming