C++ Environment Setup
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// Save this file as: main.cpp
#include <iostream>
int main() {
std::cout << "C++ source files must use the .cpp extension" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// g++ filename.cpp -o filename
#include <iostream>
int main() {
std::cout << "Compiling parses, translates, and links this file" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: