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

C++ का Environment Setup

C++ setup करना अपनी workshop तैयार करने जैसा है: आप एक compiler install करते हैं जो आपकी instructions को एक program में बदलता है, अपना code लिखते हैं, और result run करते हैं।

Compiler का Overview

एक compiler ज़रूरी है क्योंकि आपका operating system C++ source code सीधे नहीं समझता — इसे पहले machine instructions में translate करना होता है।

G++, GNU Compiler Collection का हिस्सा, Linux पर सबसे widely used C++ compiler है और अलग-अलग toolchains के जरिए Windows और macOS पर भी available है।

उदाहरण: 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 और IDEs

आप syntax highlighting और IntelliSense के लिए C/C++ extension वाले VS Code जैसे एक lightweight editor में C++ लिख सकते हैं, या एक पूरे IDE में जो एक compiler और debugger साथ bundle करता है।

अगर आप local setup पूरी तरह skip करना चाहें, cookiescursor.com का online workspace आपको सीधे browser में C++ लिखने और चलाने देता है।

उदाहरण: 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;
}

अपना Code लिखना

C++ source files हमेशा .cpp extension इस्तेमाल करती हैं, जो आपके compiler (और दूसरे developers) को signal करती है कि file plain text या किसी दूसरी language की बजाय C++ code रखती है।

गलत extension से save करना build tools को file को पूरी तरह skip करने या इसे गलत treat करने का कारण बन सकता है।

उदाहरण: 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;
}

अपना Code Compile करना

Compile करना आपके terminal में g++ filename.cpp -o filename जैसी एक command चलाकर आपकी .cpp file को एक executable में बदलता है, जो आपके code को parse, translate, और link करके machine instructions बनाता है।

आपकी source में कोई भी syntax errors इस step पर report होती हैं, program के कभी चलने का मौका मिलने से पहले।

उदाहरण: 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;
}

Output Run करना

एक successful compilation एक binary produce करता है — default से Linux/macOS पर a.out या Windows पर a.exe — जिसे आप अपने program का actual output देखने के लिए terminal से सीधे run करते हैं।

अगर आपको उम्मीद वाला result न दिखे, bug आपकी compiler setup की बजाय आपके program logic में है, क्योंकि compilation पहले ही succeed हो चुका है।

उदाहरण: 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. #}
आम गलतियां
  1. gcc से compile करना और std::cout के बारे में linker errors पाना, क्योंकि C++ programs को g++ (या -lstdc++ के साथ gcc) से build करना चाहिए।
  2. compiler चलाने से पहले file save करना भूल जाना, ताकि program का पुराना version compile और run हो।
  3. main.cpp को program की तरह run करने की कोशिश करना इसकी बजाय ./a.out या main.exe जैसा compiled output run करने की।

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.