Getting Started
Getting Started
Install a compiler, write your first program, and build from the command line.
Getting Started
Install a compiler, write your first program, and build from the command line.
Use one of these toolchains:
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
}
Save it as main.cpp.
g++ -std=c++23 -Wall -Wextra -pedantic main.cpp -o app
cl /std:c++23 /W4 main.cpp
./app
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.
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.
#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.
project/
src/
main.cpp
include/
build/
Start small, but keep generated files out of your source directories.
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.
At a high level, a normal C or C++ build goes through these stages:
#include, macros, and conditional compilationThat mental model helps explain common errors such as missing headers, undefined references, and duplicate symbols.
#include for a standard library typeWhen a build fails, read the first error carefully before chasing later cascading messages.
Even tiny programs benefit from learning a few standard components early:
std::string for textstd::vector for dynamic collectionsstd::optional for maybe-a-value resultsstd::span for non-owning views into contiguous dataThose types let you avoid many raw arrays, manual memory tricks, and ad hoc error codes.
Use this site in three layers:
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.
#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.
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";
}