EZ Notes Logo

EZNotes

🎯 Aim

To learn how to compile and run a C/C++ program using Visual Studio Code.

🛠️ What You Need

  • Visual Studio Code installed on your computer
  • C/C++ extension installed in VS Code
  • MinGW (g++) or any C++ compiler installed
  • A C++ program file to compile

📖 Introduction

Compiling is the process of converting your source code (the code you write) into a program that the computer can run. When you compile a C/C++ program, the compiler checks your code for errors and creates an executable file. In this practical, you will learn how to compile and run your C++ programs using VS Code.

📝 Steps

Step 1: Open VS Code

  1. Click the VS Code icon on your desktop or Start Menu.
  2. Open your folder where you saved your C++ programs.

Step 2: Open Your Program File

  1. In the Explorer, click on your firstprogram.cpp file.
  2. The file will open in the editor.
Opening C++ File in VS Code Figure 1: Opening a C++ File in VS Code

Step 3: Compile the Program

  1. Make sure your program is saved (Ctrl + S).
  2. Click the Terminal menu at the top.
  3. Click "New Terminal" or press Ctrl + `.
  4. Type the following command and press Enter:
g++ firstprogram.cpp -o firstprogram

Explanation:
g++ — the C++ compiler
firstprogram.cpp — your source code file
-o firstprogram — the output executable file name

Compiling C++ Program in VS Code Figure 2: Compiling the C++ Program

Step 4: Run the Program

  1. After compilation is complete, type the following command to run your program:
./firstprogram

Note: On Windows, you would type:

firstprogram.exe
Running C++ Program in VS Code Figure 3: Running the Compiled Program

Step 5: Make a Change and Recompile

  1. Change the text in your program (e.g., add a new cout line).
  2. Save the file (Ctrl + S).
  3. Compile and run again using the same commands.

📊 Observations

  • The terminal opened successfully.
  • The program was compiled using the g++ command.
  • An executable file firstprogram (or firstprogram.exe) was created.
  • The program ran and displayed the correct output.
  • Changes in the code were reflected after recompiling.

✅ Result

You have successfully compiled and run a C++ program in VS Code. You now know how to use the terminal to compile and execute your programs. This is an important skill for every programmer.

💡 Tip: If you see errors during compilation, check your code for mistakes. The compiler will tell you which line has an error.
📌 Important: Always compile after saving your file. The compiler only sees the saved version of your code.

🎯 مقصد

Visual Studio Code کا استعمال کرتے ہوئے C/C++ پروگرام کو کمپائل اور رن کرنا سیکھنا۔

🛠️ آپ کو کیا چاہیے

  • آپ کے کمپیوٹر پر Visual Studio Code انسٹال ہو
  • VS Code میں C/C++ ایکسٹینشن انسٹال ہو
  • MinGW (g++) یا کوئی بھی C++ کمپائلر انسٹال ہو
  • کمپائل کرنے کے لیے ایک C++ پروگرام فائل

📖 تعارف

کمپائل کرنا وہ عمل ہے جس میں آپ اپنے سورس کوڈ (جو آپ لکھتے ہیں) کو ایک پروگرام میں تبدیل کرتے ہیں جسے کمپیوٹر چلا سکتا ہے۔ جب آپ C/C++ پروگرام کمپائل کرتے ہیں، تو کمپائلر آپ کے کوڈ میں غلطیاں چیک کرتا ہے اور ایک ایگزیکیوٹیبل فائل بناتا ہے۔ اس عملی میں، آپ VS Code کا استعمال کرتے ہوئے اپنے C++ پروگراموں کو کمپائل اور رن کرنا سیکھیں گے۔

📝 اقدامات

پہلا قدم: VS Code کھولیں

  1. اپنے ڈیسک ٹاپ یا اسٹارٹ مینو پر VS Code آئیکن پر کلک کریں۔
  2. اپنا وہ فولڈر کھولیں جہاں آپ نے اپنے C++ پروگرام محفوظ کیے ہیں۔

دوسرا قدم: اپنی پروگرام فائل کھولیں

  1. Explorer میں، اپنی firstprogram.cpp فائل پر کلک کریں۔
  2. فائل ایڈیٹر میں کھل جائے گی۔
Opening C++ File in VS Code شکل 1: VS Code میں C++ فائل کھولنا

تیسرا قدم: پروگرام کمپائل کریں

  1. یقینی بنائیں کہ آپ کا پروگرام محفوظ ہے (Ctrl + S)۔
  2. اوپر Terminal مینو پر کلک کریں۔
  3. "New Terminal" پر کلک کریں یا Ctrl + ` دبائیں۔
  4. نیچے دی گئی کمانڈ ٹائپ کریں اور Enter دبائیں:
g++ firstprogram.cpp -o firstprogram

وضاحت:
g++ — C++ کمپائلر
firstprogram.cpp — آپ کی سورس کوڈ فائل
-o firstprogram — آؤٹ پٹ ایگزیکیوٹیبل فائل کا نام

Compiling C++ Program in VS Code شکل 2: C++ پروگرام کمپائل کرنا

چوتھا قدم: پروگرام چلائیں

  1. کمپائل مکمل ہونے کے بعد، اپنا پروگرام چلانے کے لیے نیچے دی گئی کمانڈ ٹائپ کریں:
./firstprogram

نوٹ: ونڈوز پر، آپ ٹائپ کریں گے:

firstprogram.exe
Running C++ Program in VS Code شکل 3: کمپائل شدہ پروگرام چلانا

پانچواں قدم: تبدیلی کریں اور دوبارہ کمپائل کریں

  1. اپنے پروگرام میں متن تبدیل کریں (مثال: ایک نئی cout لائن شامل کریں)۔
  2. فائل کو محفوظ کریں (Ctrl + S)۔
  3. دوبارہ کمپائل اور رن کریں۔

📊 مشاہدات

  • ٹرمینل کامیابی سے کھل گیا۔
  • پروگرام کو g++ کمانڈ کا استعمال کرتے ہوئے کمپائل کیا گیا۔
  • ایک ایگزیکیوٹیبل فائل firstprogram (یا firstprogram.exe) بنائی گئی۔
  • پروگرام چلا اور درست آؤٹ پٹ دکھایا۔
  • کوڈ میں تبدیلیاں دوبارہ کمپائل کرنے کے بعد ظاہر ہوئیں۔

✅ نتیجہ

آپ نے VS Code میں C++ پروگرام کو کامیابی سے کمپائل اور چلایا ہے۔ اب آپ جانتے ہیں کہ ٹرمینل کا استعمال کرتے ہوئے اپنے پروگراموں کو کیسے کمپائل اور چلانا ہے۔ یہ ہر پروگرامر کے لیے ایک اہم مہارت ہے۔

💡 مشورہ: اگر کمپائل کے دوران غلطیاں نظر آئیں تو اپنے کوڈ میں غلطیوں کو چیک کریں۔ کمپائلر آپ کو بتائے گا کہ کس لائن میں غلطی ہے۔
📌 اہم: فائل کو محفوظ کرنے کے بعد ہی ہمیشہ کمپائل کریں۔ کمپائلر صرف آپ کے کوڈ کا محفوظ شدہ ورژن دیکھتا ہے۔
⬅ Back to Practical Notebooks