Skip to content

Module 1 · Getting Started with C++ — topic 2 of 3

C++ Environment Setup

Install g++ or clang++, configure VS Code, choose a language standard and learn the compile–run–debug cycle for single and multi-file C++ programs.

Beginner 13 min read·Lesson 2 of 40 in C++ Programming

Learning objectives

  • Install a C++ compiler on Windows, macOS or Linux
  • Compile with a chosen standard and warnings
  • Build programs from several source files

Definition

A C++ development environment consists of a standard-conforming compiler (g++, clang++ or MSVC), a build tool, an editor or IDE, and a debugger.

Introduction

C++ uses the same toolchains as C. On Windows install MSYS2 and run pacman -S mingw-w64-ucrt-x86_64-gcc; on macOS run xcode-select --install; on Ubuntu run sudo apt install build-essential. Each gives you g++ (or clang++).

Visual Studio Community (Windows) bundles Microsoft's MSVC compiler and an excellent debugger; VS Code with the C/C++ extension works on every platform.

Why it matters

Compilers default to different C++ standards. Selecting one explicitly with -std=c++17 means your code behaves the same in every lab and on every machine.

Knowing how to compile multiple files from the command line is essential for every project in this course and for understanding build systems such as CMake.

Detailed explanation

Compile and link in one step: g++ -std=c++17 -Wall -Wextra main.cpp -o app. Run with ./app (Linux/macOS) or .\app.exe (Windows).

For several files: g++ -std=c++17 main.cpp account.cpp -o app. Each .cpp file is a translation unit compiled separately; the linker joins them.

-g adds debug information for gdb or lldb; -O2 optimises; -fsanitize=address,undefined detects memory errors and undefined behaviour while testing.

PlatformInstallCompiler command
WindowsMSYS2 (UCRT64) or Visual Studiog++ / cl
macOSXcode command-line toolsclang++ (g++ is an alias)
Linuxbuild-essentialg++

Syntax

Terminal
g++ -std=c++17 -Wall -Wextra -g main.cpp -o app
./app
  • Use .cpp as the source extension and .hpp or .h for headers.
  • Recompile after every change.
  • Add -fsanitize=address while testing on Linux/macOS.

Code examples

Checking the compiler

Terminal
g++ --version

Prints the compiler version, e.g. g++ (GCC) 14.2.0.

Which standard am I compiling?

C++
#include <iostream>
int main() {
std::cout << (__cplusplus >= 201703L ? "C++17 or newer" : "older than C++17") << '\n';
return 0;
}
Output
C++17 or newer

__cplusplus is 201703L for C++17 and 202002L for C++20.

More examples

A warning that catches a real bug

C++
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {4, 8, 15};
int sum = 0;
for (std::size_t i = 0; i < v.size(); ++i)
sum += v[i];
std::cout << "sum = " << sum << '\n';
return 0;
}
Output
sum = 27

Writing for (int i = 0; i < v.size(); ++i) triggers -Wsign-compare because size() is unsigned; using std::size_t (or a range-for) removes the warning.

Step-by-step explanation

  • std::vector<int> v = {4, 8, 15}; creates a vector with three elements.
  • std::size_t is the unsigned type returned by size(), so comparisons are consistent.
  • The loop adds 4 + 8 + 15 = 27.
  • With -Wall -Wextra, mixing int and size_t in the condition would produce a warning.

Expected output

Running “A warning that catches a real bug” prints exactly:

TextOutput
sum = 27

Practical applications

  • Industrial projects build with CMake, which generates the same compiler commands.
  • Continuous-integration servers compile with warnings as errors (-Werror).
  • Game studios compile with MSVC on Windows and clang on consoles.

Best practices

  • Use the same flags for every exercise.
  • Fix all warnings before running the program.
  • Learn three debugger commands: break, run, print.

Common mistakes

Compiling C++ with gcc

Avoid

gcc main.cpp -o app

Prefer

g++ main.cpp -o app

gcc compiles .cpp as C++ but does not link the C++ standard library, causing errors such as undefined reference to std::cout.

Not choosing a standard

Avoid

g++ main.cpp -o app

Prefer

g++ -std=c++17 main.cpp -o app

Older compilers default to C++98 or C++14, so modern features may fail to compile.

Important notes

  • On Windows with MSYS2, add C:\msys64\ucrt64\bin to PATH so g++ works in PowerShell.
  • Online compilers are convenient for quick checks but do not replace a local toolchain for projects.

Key takeaways

  • Install g++/clang++ for your OS.
  • Compile with g++ -std=c++17 -Wall -Wextra files -o app.
  • Use g++ (not gcc) to link the C++ library.

Try it yourself

  1. 1
    Install a compiler and print the value of __cplusplus with -std=c++17 and -std=c++20.
  2. 2
    Split a program into main.cpp and math.cpp and compile them together.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

2 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhy does `gcc main.cpp -o app` often fail with errors like “undefined reference to std::cout”?
  • BeginnerWhat does the flag `-std=c++17` do?
Practise this topic