xSKILLby Infirals
Register

C++ for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

How C++ becomes a program

Starting out10 min readLesson 1 of 6

1.1 From source to executable

A C++ program starts as text in one or more source files. The compiler turns that text into machine instructions, and the linker joins the results into an executable file. A function declaration without a definition can pass compilation and then fail at link time because these are separate stages.

C++ is statically typed. The compiler checks names, types, and function calls before the program runs. You find many mistakes earlier than you would in a language that waits until runtime.

cpp
#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
    return 0;
}

Output

Hello, world!

1.1.1 What each stage does

  1. Preprocessing expands #include lines, macros, and conditional blocks. Headers such as <iostream> bring declarations into the source.
  2. Compilation translates each source file into an object file. Syntax errors and type errors appear here.
  3. Linking connects object files and libraries to make the final executable. Missing definitions and duplicate symbols appear here.

A translation unit is one source file after preprocessing. Projects with several source files compile each translation unit separately, which keeps rebuilds manageable.

cpp
#include <iostream>

#define BUILD 3

int main() {
    std::cout << "Build " << BUILD << "\n";
    std::cout << "Preprocessing finished.\n";
    return 0;
}

Output

Build 3
Preprocessing finished.

1.1.2 Linking and libraries

The standard library ships with the compiler and supplies common code such as input and output streams, strings, and math functions. A library is a collection of compiled code plus declarations that tell the compiler what can be called.

When the linker cannot find a definition, the error often mentions the function name. Check the spelling, the parameter types, and whether every source file reached the build command. A library can also need an extra flag on the command line.

cpp
#include <cmath>
#include <iostream>

int main() {
    double root = std::sqrt(81.0);
    std::cout << "Square root: " << root << "\n";
    return 0;
}

Output

Square root: 9

1.1.3 Build a first program

Save the source as hello.cpp. The exact command depends on the toolchain, and the standard flag selects the language version.

  • GCC or Clang: g++ -std=c++17 -Wall -Wextra hello.cpp -o hello
  • MSVC from a developer prompt: cl /EHsc /std:c++17 hello.cpp

Run ./hello on Linux or macOS, or hello.exe on Windows. The flags -Wall and -Wextra turn on common warnings. A warning reports code that still compiled, and it often marks a real mistake.

1.1.4 Read warnings

An unused variable may be harmless in a practice file and suspicious in a longer function. A signed and unsigned comparison can misbehave when a negative value turns into a large unsigned number.

Fix warnings while the code is small. Rebuild with warnings enabled after changes, and avoid adding new warnings to a file that had none.

cpp
#include <iostream>

int main() {
    int unused = 42;
    std::cout << "Warnings point at suspicious code.\n";
    return 0;
}

Output

Warnings point at suspicious code.

1.1.5 Read the first error

Compiler messages usually include the file, line, and a short description. Read the first error before the ones below it, because one missing semicolon can trigger several later complaints. Typical beginner messages include:

  • expected ';' before ...: a statement or class definition is missing a semicolon.
  • 'cout' was not declared in this scope: the header or the std:: prefix is missing.
  • undefined reference to ...: a function is declared and called, but no definition reached the linker.

The build process also reports an exit code; 0 usually means success. Copy the exact command and the first error when you ask for help.

1.1.6 Standards and compiler choice

The examples in this course use C++17 as their baseline. C++20 adds language and library features, and most examples also build with -std=c++20. On MSVC the equivalent flag is /std:c++20. Each compiler offers small extensions, so test code with the standard you plan to use.

An editor can help with indentation, and the build command remains the source of truth. Keep a note with the exact command that works on your machine.

1.1.7 Practice

  1. Save the hello program, compile it with warnings enabled, and run it.
  2. Delete a semicolon and rebuild. Record the first error.
  3. Add an unused variable and compare the warning output.
  4. Restore the file and write down the build command you used.

A failed build still teaches you which stage rejected the code. Read the message before changing several lines at once.

Interview questions

3 questions from this note. Open one to see the answer; no account needed.

  • 01What is the difference between compiling and linking?

    Compiling turns one translation unit into an object file and checks syntax and types. Linking joins object files and libraries into an executable and resolves external names.

  • 02Why does std::cout fail to compile when #include <iostream> is missing?

    The declaration of std::cout comes from the <iostream> header. Without that header, the compiler has not seen the name.

  • 03What does an undefined reference error usually mean?

    The code used a function that the linker could not find a definition for. Check the function name, its parameter types, and whether its source file was included in the build.

Quiz

Check: building a C++ program

Four questions on the build stages, translation units and where link errors appear.

4 questions

Question 1 of 4

0/4 answered

A function declaration without a definition passes compilation but fails at:

Answer all you can, then submit.