Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

headers-compilation-cpp

Headers and compilation

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Headers and compilation: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship Headers and compilation in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Headers and compilation in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C++ programs compile in translation units (.cpp files). Headers declare interfaces; one .cpp file provides each non-inline definition the linker needs—similar discipline to multi-file C projects.

Include guards

#ifndef WIDGET_H
#define WIDGET_H
// declarations
#endif

Or #pragma once on supported compilers. The playground uses a single main.cpp, but local projects split headers and sources.

Build pipeline

  1. Preprocessor expands #include and macros
  2. Compiler emits object files per translation unit
  3. Linker resolves symbols into an executable

Important interview questions and answers

  1. Q: Undefined reference?
    A: Linker error—function declared but not defined, or missing library.
  2. Q: ODR?
    A: One Definition Rule—each non-inline function/variable must have exactly one definition across the program.

Self-check

  1. Why use include guards?
  2. What does the linker do?

Tip: One Definition Rule—inline functions in headers OK; non-inline function definitions belong in exactly one .cpp file.

Interview prep

Include guards purpose?

Prevent the same header from being processed twice, avoiding duplicate definitions.

Undefined reference?

Linker error—function declared but not defined, or required library not linked.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Header guards?
  • ODR violation?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump