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.
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.
Syntax
- Use
.cppas the source extension and.hppor.hfor headers. - Recompile after every change.
- Add
-fsanitize=addresswhile testing on Linux/macOS.
Code examples
Checking the compiler
Prints the compiler version, e.g. g++ (GCC) 14.2.0.
Which standard am I compiling?
C++17 or newer
__cplusplus is 201703L for C++17 and 202002L for C++20.
More examples
A warning that catches a real bug
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_tis the unsigned type returned bysize(), so comparisons are consistent.- The loop adds 4 + 8 + 15 = 27.
- With
-Wall -Wextra, mixingintandsize_tin the condition would produce a warning.
Expected output
Running “A warning that catches a real bug” prints exactly:
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
Prefer
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
Prefer
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\binto PATH sog++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
- 1Install a compiler and print the value of
__cpluspluswith-std=c++17and-std=c++20. - 2Split a program into
main.cppandmath.cppand 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?