C++ Lesson 3 – Setup & Installation | Dataplexa
Foundations Β· Lesson 3

Setup & Installation

Configure your development environment and run your first compilation commands

The Modern C++ Toolchain

C++ development requires a compiler β€” software that transforms your human-readable code into machine instructions. Unlike interpreted languages such as Python or JavaScript, C++ code must be compiled before it can run.

The most common C++ compilers are g++ (GNU Compiler Collection) and clang++ (LLVM). Both support C++20 β€” the modern standard that powers everything from Tesla's autopilot software to Chrome's V8 engine.

GCC (g++)

GNU compiler. Free, open source. Default on Linux. Excellent error messages and debugging support.

Clang (clang++)

LLVM compiler. Fast compilation. Default on macOS. Powers Xcode and many IDEs.

Both compilers produce identical results for standard C++ code. Your choice depends on your operating system and personal preference.

Platform-Specific Installation

Windows Setup

Windows doesn't include a C++ compiler by default. The easiest approach uses MinGW-w64 β€” a Windows port of the GNU compiler collection.

1
Download MinGW-w64 from winlibs.com
2
Extract to C:\mingw64
3
Add C:\mingw64\bin to your PATH environment variable
4
Open Command Prompt and run: g++ --version

Adding to PATH tells Windows where to find the compiler. Without this step, you'd need to type the full path every time: C:\mingw64\bin\g++ instead of just g++.

macOS Setup

macOS includes Xcode Command Line Tools, which provide clang++ β€” Apple's version of the LLVM compiler. Open Terminal and run:

xcode-select --install

This downloads about 500MB of development tools. Once complete, verify the installation:

clang++ --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.0.0
Thread model: posix

The clang++ command works identically to g++. Both compile C++ code using the same command-line options.

Linux Setup

Most Linux distributions include g++ in their package repositories. Ubuntu and Debian users:

sudo apt update
sudo apt install g++

Red Hat, Fedora, and CentOS users:

sudo dnf install gcc-c++

Verify installation with:

g++ --version

Linux distributions typically ship recent compiler versions. GCC 11 or newer supports all C++20 features we'll use.

Your First Compilation

Create a simple program to test your compiler setup. Open any text editor and save this as hello.cpp:

#include 
using namespace std;

int main() {
    cout << "C++ is working!" << endl;
    return 0;
}

Now compile and run it. Navigate to the file's directory in your terminal or command prompt:

$ g++ -std=c++20 -o hello hello.cpp && ./hello
C++ is working!

What just happened?

g++ β€” invokes the GNU C++ compiler

-std=c++20 β€” tells the compiler to use C++20 language features

-o hello β€” names the output executable "hello"

hello.cpp β€” the source file to compile

Try this: Change the message to "Hello, world!" and recompile

Windows Executable Note

On Windows, use hello.exe instead of ./hello:

g++ -std=c++20 -o hello.exe hello.cpp
hello.exe

The .exe extension is required on Windows. Unix-like systems (Linux and macOS) don't need file extensions for executables.

Essential Compiler Flags

Professional C++ development uses additional compiler flags for debugging, optimization, and safety. Here are the most important ones:

// Debug build - includes debugging symbols, no optimization
g++ -std=c++20 -g -Wall -Wextra -o debug_program program.cpp

// Release build - maximum optimization, no debugging symbols
g++ -std=c++20 -O3 -DNDEBUG -o release_program program.cpp

Flag breakdown:

-g β€” includes debugging information for debuggers like GDB

-Wall β€” enables common warnings (Wall = "warn all")

-Wextra β€” enables additional warnings beyond -Wall

-O3 β€” maximum optimization level

-DNDEBUG β€” disables assert() statements in release builds

Use debug builds during development. They're slower but catch more errors. Release builds are for production β€” they run faster but hide debugging information.

Common Beginner Mistake

Mistake: Forgetting -std=c++20 and using outdated C++98 features

Error: Modern syntax like auto and range-based loops won't work

Fix: Always specify the standard version explicitly

Text Editors and IDEs

You can write C++ in any text editor, but modern IDEs provide syntax highlighting, error detection, and integrated debugging. Popular choices include:

Editor/IDE Best For Key Features
VS Code Beginners Free, lightweight, excellent C++ extension
CLion Professionals Advanced debugging, refactoring, CMake support
Qt Creator GUI development Integrated Qt framework, visual designers
Notepad++ Simple editing Windows only, fast, minimal features

For beginners, VS Code with the C/C++ extension provides the best balance of features and simplicity. Download it from code.visualstudio.com.

VS Code C++ Setup

After installing VS Code, add the official C/C++ extension:

1
Open VS Code and press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (macOS)
2
Search for "C/C++" by Microsoft
3
Click Install

This extension provides IntelliSense (code completion), error highlighting, and integrated debugging. It automatically detects your compiler and suggests build configurations.

Online Development Options

Don't want to install anything locally? Several online compilers support C++20:

πŸš€ Compiler Explorer (godbolt.org)

Shows assembly output, supports multiple compilers, perfect for learning how code compiles.

πŸ’» Replit

Full IDE in browser, collaborative editing, project templates available.

Compiler Explorer is particularly valuable for understanding optimization. You can see exactly how the compiler transforms your C++ code into machine instructions.

Pro tip: Use online compilers for quick experiments and learning. Install a local compiler for serious development work.

Testing Your Online Setup

Visit godbolt.org and select "GCC 13.2" from the compiler dropdown. Paste this code:

#include 
#include 
using namespace std;

int main() {
    vector numbers = {1, 2, 3, 4, 5};
    
    for (const auto& num : numbers) {
        cout << num << " ";
    }
    
    return 0;
}

Make sure the compiler flags include -std=c++20. The output should show:

1 2 3 4 5

This code uses modern C++20 features like auto and range-based for loops. If it compiles and runs, your environment supports everything we'll cover.

What command compiles a C++ file named "program.cpp" using C++20 standard?


What does the -Wall compiler flag do?


Which statement about C++ compiler availability is correct?


Up Next

First C++ Program

Write and understand a complete C++ program from main() function to output statements