C++ ยท Chapter 2 of 49

C++ Install & Setup

To compile C++ you need a compiler such as GCC (g++), Clang, or MSVC on Windows. Many Linux distros ship g++ already; on Mac you can install Xcode command line tools, and on Windows you can use MinGW or Visual Studio.

Once installed, you compile a `.cpp` file into an executable and then run that executable separately โ€” unlike interpreted languages, there's a distinct build step.

Syntax
g++ file.cpp -o file && ./file

Verify the install

Run `g++ --version` in a terminal. If it prints a version number, you're ready to compile C++ code.

Editors and IDEs

VS Code with the C/C++ extension, CLion, and Code::Blocks are popular. Competitive programmers often use a single main.cpp with a terminal build command.

Example 1 (bash)
g++ --version
Output
g++ (GCC) 13.2.0

Shows the installed GCC/g++ version.

Example 2 (bash)
g++ hello.cpp -o hello
./hello
Output
Hello, World!

Compiles hello.cpp into an executable named hello, then runs it.

Key points

  • Install g++ (GCC), Clang, or MSVC.
  • Compiling and running are two separate steps.
  • Use `g++ file.cpp -o out` to build an executable.
  • VS Code + C/C++ extension is a common free setup.
๐Ÿ’ก Note: On competitive programming judges, always compile with the same standard flag (e.g. -std=c++17) you plan to submit with.

๐Ÿ“ Quick Quiz

1. Which command compiles main.cpp into an executable named app?

2. A common free C++ compiler is:

3. C++ requires a separate ___ step before running.