November 7, 2025
Tech Post

How to run first C++ program using Visual Studio Code

Here’s a step-by-step guide to run your first C++ program in Visual Studio Code (VS Code) — perfect if you’re just getting started 👇


🧰 Step 1: Install the required tools

1. Install VS Code

2. Install a C++ compiler

Depending on your operating system:

  • Windows:
    Install MinGW or MSYS2
    • Easiest option: MSYS2
    • After installation, open the MSYS2 terminal and run: pacman -S mingw-w64-ucrt-x86_64-gcc
    • Add C:\msys64\ucrt64\bin (or your MinGW bin folder) to your PATH environment variable.
  • macOS:
    Install Xcode command-line tools: xcode-select --install
  • Linux (Ubuntu/Debian): sudo apt update sudo apt install build-essential

Check the installation:

g++ --version

If it shows a version number, your compiler is ready ✅


🧩 Step 2: Set up VS Code for C++

  1. Open VS Code.
  2. Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X).
  3. Search for and install:
    • C/C++ (by Microsoft)
    • Code Runner (optional — makes running code easier)

💻 Step 3: Write your first C++ program

  1. Create a new folder for your project (e.g., C:\cpp_projects\hello_world)
  2. Open that folder in VS Code (File → Open Folder)
  3. Create a new file: hello.cpp
  4. Write this code: #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }

⚙️ Step 4: Compile and run

Option 1 — Using the terminal (recommended)

  1. Open the terminal in VS Code:
    Ctrl + ~ (tilde key)
  2. Compile the code: g++ hello.cpp -o hello
  3. Run the executable:
    • On Windows: hello.exe
    • On macOS/Linux: ./hello

✅ You should see:

Hello, World!

Option 2 — Using the Code Runner extension

If you installed Code Runner, simply click the ▶ Run Code button at the top right or press:

Ctrl + Alt + N

Your output will appear in the “Output” pane.


🧠 Optional: Create a Build Task

If you want to automate the build/run steps:

  1. Go to Terminal → Configure Default Build Task → C/C++: g++.exe build active file
  2. VS Code will create a tasks.json file in .vscode/
  3. Next time, press Ctrl+Shift+B to build automatically.

Related posts

How to earn money from instagram full detail blog

Dheeraj Pal

How to rank a website in usa and uk countries

Dheeraj Pal

How to run first C Program using Visual Studio Code and MinGW

Dheeraj Pal

Leave a Comment