Getting Started

Getting Started

Install a compiler, write your first program, and build from the command line.

Getting Started

1. Choose a compiler

Use one of these toolchains:

2. Create a first file

#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
}

Save it as main.cpp.

3. Compile it

GCC / Clang

g++ -std=c++23 -Wall -Wextra -pedantic main.cpp -o app

MSVC Developer Command Prompt

cl /std:c++23 /W4 main.cpp

4. Run it

./app

5. Mental model

A C++ source file is compiled into machine code. The compiler checks syntax and types before you run the program.

For now, the important habit is simple: edit, compile, read warnings, run, then repeat in very small steps.

6. Next steps

7. A better first compile command

As soon as you move past "hello world", prefer separate debug and release builds.

g++ -std=c++23 -Wall -Wextra -Wpedantic -g -O0 main.cpp -o app-debug
g++ -std=c++23 -Wall -Wextra -Wpedantic -O2 main.cpp -o app-release

The debug build helps investigation. The release build shows the performance and code generation you will eventually ship.

8. A slightly more realistic first program

#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!\n";
}

This introduces input, output, variables, and string handling without adding too much complexity at once.

9. First project structure

project/
    src/
        main.cpp
    include/
    build/

Start small, but keep generated files out of your source directories.

10. Separate compilation in one glance

Real programs are usually split across more than one file.

// math.h
int add(int a, int b);
// math.cpp
int add(int a, int b) {
    return a + b;
}
// main.cpp
#include <iostream>
#include "math.h"

int main() {
    std::cout << add(2, 3) << '\n';
}

That split leads naturally to the compile-and-link mental model.

11. Habits worth adopting immediately

12. What the compiler is actually doing

At a high level, a normal C or C++ build goes through these stages:

  1. preprocessing: handles #include, macros, and conditional compilation
  2. compilation: parses the source and produces object code
  3. linking: combines object files and libraries into the final executable

That mental model helps explain common errors such as missing headers, undefined references, and duplicate symbols.

13. Common beginner errors

When a build fails, read the first error carefully before chasing later cascading messages.

14. Your first useful standard-library tools

Even tiny programs benefit from learning a few standard components early:

Those types let you avoid many raw arrays, manual memory tricks, and ad hoc error codes.

15. Where to look things up

Use this site in three layers:

16. Set up your editor or IDE next

Once the compiler is working, your next step should be a usable editing and debugging environment.

Continue with IDE Setup for C++ before you start building larger examples.

Example in practice

#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
}

Compile it first from the terminal, then move to the IDE setup tutorial once the toolchain is confirmed.

Try this variation

Change the program so it reads a name from standard input and prints a personalized greeting. That forces you to confirm both your compiler and terminal input/output setup.

#include <iostream>
#include <string>

int main() {
    std::string name;
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!\n";
}