JSTAcademy
0 XP
Dashboard
Crash Courses
Environment & Hello World
20 min
Basic+155 XP
Crash Courses · Basic

Environment & Hello World

Install a compiler, configure VS Code, and understand compile flags
20 min read+155 XP on completionCert: C++ Crash Course
Tap any word in the text below to start reading from there.

Setting Up C++

1. Install a Compiler

macOS:

xcode-select --install  # installs clang++
clang++ --version

Ubuntu/WSL:

sudo apt update && sudo apt install g++ build-essential
g++ --version

Windows (native): Install MSYS2 from https://msys2.org, then inside the MSYS2 terminal:

pacman -S mingw-w64-x86_64-gcc

2. VS Code Setup

Install the C/C++ extension by Microsoft (ms-vscode.cpptools). It provides:

  • IntelliSense (autocomplete, hover)
  • Debugger integration (via gdb/lldb)
  • Code formatting with clang-format

3. Hello World

main.cpp:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Compile and run:

g++ -std=c++17 -Wall -Wextra -o hello main.cpp
./hello
# Hello, World!

4. Essential Compile Flags

FlagPurpose
`-std=c++17`Enable C++17 features
`-Wall`Enable common warnings
`-Wextra`Enable additional warnings
`-O2`Enable optimizations (release)
`-g`Include debug symbols
`-o output`Specify output filename

5. The Compilation Pipeline

Source (.cpp)
     ↓ Preprocessor   #includes resolved, macros expanded
     ↓ Compiler        → assembly (.s)
     ↓ Assembler       → object file (.o)
     ↓ Linker          → executable

6. Using namespace std

#include <iostream>
using namespace std;

int main() {
    cout << "Shorter syntax" << endl;
    return 0;
}

The using namespace std directive lets you write cout instead of std::cout. Avoid it in header files (it pollutes the namespace of every file that includes the header).

7. Namespaces

namespace myapp {
    void greet() {
        std::cout << "Hello from myapp!" << std::endl;
    }
}

int main() {
    myapp::greet();
}
0%