← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 3 of 15

C++ Environment Setup

Setting up C++ is like getting your workshop ready: you install a compiler that turns your instructions into a program, write your code, and run the result.

Compiler Overview

A compiler is required because C++ source code isn't understood directly by your operating system — it has to be translated into machine instructions first.

G++, part of the GNU Compiler Collection, is the most widely used C++ compiler on Linux and is also available on Windows and macOS through various toolchains.

Example: Compiler Overview

cpp
// g++ is the most widely used C++ compiler on Linux
// (also available on Windows/macOS via various toolchains)
#include <iostream>

int main() {
	std::cout << "Translated to machine code by a compiler like g++" << std::endl;
	return 0;
}

Text Editors and IDEs

You can write C++ in a lightweight editor like VS Code with the C/C++ extension for syntax highlighting and IntelliSense, or in a full IDE that bundles a compiler and debugger together.

If you'd rather skip local setup entirely, cookiescursor.com's online workspace lets you write and run C++ directly in the browser.

Example: Text Editors and IDEs

cpp
#include <iostream>

int main() {
	// Write in VS Code + C/C++ extension, a full IDE, or
	// cookiescursor.com's online workspace -- no local setup needed.
	std::cout << "Any editor works, as long as a C++ compiler is available" << std::endl;
	return 0;
}

Writing Your Code

C++ source files always use the .cpp extension, which signals to your compiler (and to other developers) that the file contains C++ code rather than plain text or another language.

Saving with the wrong extension can cause build tools to skip the file entirely or treat it incorrectly.

Example: Writing Your Code

cpp
// Save this file as: main.cpp
#include <iostream>

int main() {
	std::cout << "C++ source files must use the .cpp extension" << std::endl;
	return 0;
}

Compiling Your Code

Compiling turns your .cpp file into an executable by running a command like g++ filename.cpp -o filename in your terminal, which parses, translates, and links your code into machine instructions.

Any syntax errors in your source will be reported at this step, before the program ever gets a chance to run.

Example: Compiling Your Code

cpp
// g++ filename.cpp -o filename
#include <iostream>

int main() {
	std::cout << "Compiling parses, translates, and links this file" << std::endl;
	return 0;
}

Running the Output

A successful compilation produces a binary — a.out on Linux/macOS or a.exe on Windows by default — which you run directly from the terminal to see your program's actual output.

If you don't see the result you expect, the bug is in your program logic rather than your compiler setup, since compilation already succeeded.

Example: Running the Output

cpp
// After compiling: ./a.out (Linux/macOS) or a.exe (Windows)
#include <iostream>

int main() {
	std::cout << "This is the program's actual output" << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Compiling with gcc and getting linker errors about std::cout, because C++ programs should be built with g++ (or gcc with -lstdc++).
  2. Forgetting to save the file before running the compiler, so the old version of the program is compiled and run.
  3. Running main.cpp as if it were the program instead of running the compiled output such as ./a.out or main.exe.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.