🟢 Foundation  ·  Lesson 03

C++ Setup: GCC, MinGW, VS Code and Online Compiler

What you need to run C++

To write and run C++ you need two things: a compiler (which turns your code into a runnable program) and a place to write the code (a text editor or IDE). The most widely used free compiler is g++, part of the GCC toolset.

You have three practical routes, from easiest to most complete: an online compiler, a local compiler with an editor, or a full IDE. We will cover each.

Fastest start: an online compiler

If you just want to start learning today, an online C++ compiler needs nothing installed. You type code in the browser, press Run, and see the output. It is ideal for the early lessons in this course. When your programs grow, move to a local setup below.

Windows: MinGW (GCC)

Windows has no built-in C++ compiler, so you install MinGW, which provides g++.

  1. Download and install MinGW (or the MSYS2 version of it).
  2. During install, include the C++ compiler component.
  3. Add its bin folder to your system PATH so g++ works in any terminal.
  4. Open a new Command Prompt and type g++ --version to check it works.
💡 Check the PATH

If g++ is "not recognised," the PATH step was missed. Add the MinGW bin folder to PATH and reopen the terminal.

Linux and macOS: GCC

On most Linux systems the compiler is one command away:

Terminal (Ubuntu/Debian)
sudo apt update
sudo apt install g++

On macOS, installing Xcode Command Line Tools with xcode-select --install gives you a C++ compiler (clang, which understands the same commands).

Using VS Code

VS Code is a free, lightweight editor. Combined with a compiler like g++, it is a comfortable setup:

  1. Install VS Code and the official C/C++ extension.
  2. Make sure g++ is installed and on your PATH (see above).
  3. Open your .cpp file and use the built-in terminal to compile and run.

Compiling and running

Whatever setup you use, the compile-and-run steps are the same. Save this as hello.cpp:

hello.cpp
#include <iostream>
using namespace std;
int main() {
    cout << "It works!" << endl;
    return 0;
}

Then in the terminal:

Terminal
g++ hello.cpp -o hello    # compile
./hello                   # run (Windows: hello.exe)
Output:
It works!

Common setup problems

  • "g++ is not recognised" — the compiler is not on your PATH; add its bin folder and reopen the terminal.
  • Editing the file but running an old compiled program — recompile after every change.
  • Saving with the wrong extension — the file must end in .cpp.
  • Forgetting to reopen the terminal after changing PATH, so it still cannot find g++.
🏋️ Practice

Get any one setup working — online is fine to start — and successfully compile and run the hello.cpp above. Once you see "It works!", you are ready for the first C++ program lesson.

Summary

  • Running C++ needs a compiler (usually g++) and an editor.
  • An online compiler needs nothing installed and is great to start.
  • On Windows, MinGW provides g++; on Linux/macOS it is one install command.
  • VS Code plus the C/C++ extension makes a comfortable local setup.
  • Compile with g++ file.cpp -o name, then run the program.

Frequently Asked Questions

What do I need to run C++ programs?
You need a C++ compiler, which turns your code into a program the computer can run. The most common free compiler is g++ (part of GCC). You also need a text editor or an IDE to write the code — many people use VS Code, Code::Blocks, or an online compiler.
What is the best C++ compiler for beginners?
g++ from the GCC toolset is the most popular free choice and works on Windows (via MinGW), Linux and macOS. Beginners often pair it with VS Code or use an all-in-one IDE like Code::Blocks, which bundles the compiler and editor together.
Can I run C++ without installing anything?
Yes. Online compilers let you write and run C++ in your browser with nothing to install, which is perfect for learning the basics or testing small programs. For larger projects you will eventually want a compiler installed locally.
How do I compile a C++ program with g++?
Save your code in a file such as hello.cpp, then run g++ hello.cpp -o hello to compile it into a program named hello. Run it with ./hello on Linux or macOS, or hello.exe on Windows.
What is MinGW and why is it needed on Windows?
MinGW is a package that brings the GCC compiler (including g++) to Windows. Windows does not include a C++ compiler by default, so installing MinGW gives you the g++ command that compiles your programs from the command line or an editor.

C++ चलाने के लिए क्या चाहिए

C++ लिखने और चलाने को आपको दो चीज़ें चाहिए: एक compiler (जो आपके code को चलने योग्य program में बदलता है) और code लिखने की जगह (text editor या IDE)। सबसे व्यापक रूप से इस्तेमाल होने वाला मुफ़्त compiler g++ है, GCC toolset का हिस्सा।

आपके पास तीन व्यावहारिक रास्ते हैं, सबसे आसान से सबसे पूर्ण तक: online compiler, editor के साथ local compiler, या पूरा IDE। हम हर एक को कवर करेंगे।

सबसे तेज़ शुरुआत: online compiler

अगर आप बस आज सीखना शुरू करना चाहते हैं, online C++ compiler को कुछ install करने की ज़रूरत नहीं। आप browser में code टाइप करते हैं, Run दबाते हैं, और output देखते हैं। यह इस course के शुरुआती lessons के लिए आदर्श है। जब आपके programs बढ़ें, नीचे दिए local setup पर जाएँ।

Windows: MinGW (GCC)

Windows में built-in C++ compiler नहीं है, तो आप MinGW install करते हैं, जो g++ देता है।

  1. MinGW (या उसका MSYS2 version) download और install करें।
  2. Install के दौरान C++ compiler component शामिल करें।
  3. इसके bin folder को अपने system PATH में जोड़ें ताकि g++ किसी भी terminal में काम करे।
  4. नया Command Prompt खोलें और g++ --version टाइप करके जाँचें कि यह काम करता है।
💡 PATH जाँचें

अगर g++ "not recognised" है, तो PATH कदम छूट गया। MinGW bin folder को PATH में जोड़ें और terminal दोबारा खोलें।

Linux और macOS: GCC

अधिकांश Linux systems पर compiler एक command दूर है:

Terminal (Ubuntu/Debian)
sudo apt update
sudo apt install g++

macOS पर, xcode-select --install से Xcode Command Line Tools install करना आपको C++ compiler (clang, जो वही commands समझता है) देता है।

VS Code इस्तेमाल करना

VS Code एक मुफ़्त, हल्का editor है। g++ जैसे compiler के साथ मिलकर, यह आरामदायक setup है:

  1. VS Code और आधिकारिक C/C++ extension install करें।
  2. पक्का करें कि g++ installed है और आपके PATH पर है (ऊपर देखें)।
  3. अपनी .cpp file खोलें और built-in terminal से compile तथा run करें।

Compile और run करना

आप जो भी setup इस्तेमाल करें, compile-और-run कदम वही हैं। इसे hello.cpp के रूप में सहेजें:

hello.cpp
#include <iostream>
using namespace std;
int main() {
    cout << "It works!" << endl;
    return 0;
}

फिर terminal में:

Terminal
g++ hello.cpp -o hello    # compile
./hello                   # run (Windows: hello.exe)
Output:
It works!

आम setup समस्याएँ

  • "g++ is not recognised" — compiler आपके PATH पर नहीं है; उसका bin folder जोड़ें और terminal दोबारा खोलें।
  • File edit करना पर पुराना compiled program चलाना — हर बदलाव के बाद दोबारा compile करें।
  • गलत extension से सहेजना — file .cpp पर खत्म होनी चाहिए।
  • PATH बदलने के बाद terminal दोबारा खोलना भूलना, तो वह अब भी g++ नहीं ढूँढ पाता।
🏋️ अभ्यास

कोई एक setup चालू करें — शुरू करने को online ठीक है — और ऊपर दिए hello.cpp को सफलतापूर्वक compile तथा run करें। जब आप "It works!" देखें, आप पहला C++ program lesson के लिए तैयार हैं।

सारांश

  • C++ चलाने को compiler (आमतौर पर g++) और editor चाहिए।
  • Online compiler को कुछ install करने की ज़रूरत नहीं और शुरू करने को बढ़िया है।
  • Windows पर MinGW g++ देता है; Linux/macOS पर यह एक install command है।
  • VS Code और C/C++ extension आरामदायक local setup बनाते हैं।
  • g++ file.cpp -o name से compile करें, फिर program चलाएँ।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.