C++ का Environment Setup
In this page:
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
// 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 और 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
#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
अपना 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
// 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
अपना 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
// 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
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
// 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
gccसे compile करना औरstd::coutके बारे में linker errors पाना, क्योंकि C++ programs कोg++(या-lstdc++के साथgcc) से build करना चाहिए।- compiler चलाने से पहले file save करना भूल जाना, ताकि program का पुराना version compile और run हो।
main.cppको program की तरह run करने की कोशिश करना इसकी बजाय./a.outयाmain.exeजैसा compiled output run करने की।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: