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
- Download and install from: https://code.visualstudio.com/
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 MinGWbinfolder) 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++
- Open VS Code.
- Go to Extensions (
Ctrl+Shift+XorCmd+Shift+X). - Search for and install:
- C/C++ (by Microsoft)
- Code Runner (optional — makes running code easier)
💻 Step 3: Write your first C++ program
- Create a new folder for your project (e.g.,
C:\cpp_projects\hello_world) - Open that folder in VS Code (File → Open Folder)
- Create a new file:
hello.cpp - 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)
- Open the terminal in VS Code:
Ctrl + ~(tilde key) - Compile the code:
g++ hello.cpp -o hello - Run the executable:
- On Windows:
hello.exe - On macOS/Linux:
./hello
- On Windows:
✅ 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:
- Go to Terminal → Configure Default Build Task → C/C++: g++.exe build active file
- VS Code will create a
tasks.jsonfile in.vscode/ - Next time, press Ctrl+Shift+B to build automatically.