How to Manually Compile and Run C/C++ Code in VS Code Terminal

Visual Studio Code (VS Code) is one of the most powerful and customizable code editors available today. Whether you're learning C or C++, knowing how to compile and run your code manually is an essential skill.

In this guide, I’ll show you step-by-step how to compile and run C/C++ programs using the terminal inside VS Code. Let’s go.


🧐 Why Compile Manually?

While extensions like Code Runner exist, compiling manually gives you:

✅ Full control over compilation
✅ Better understanding of how compilers work
✅ Ability to manage multi-file projects
✅ Easier debugging in the long run

If you’re serious about coding, manual is the way to go.


⚙️ Step 1: Install GCC/G++ Compiler

You need a C/C++ compiler installed on your system. Here’s how:

🖥️ For Windows Users:

  1. Download MSYS2https://www.msys2.org/

  2. Open MSYS2 terminal and run:

     pacman -S mingw-w64-ucrt-x86_64-gcc
    
  3. Add this path to your System Environment Variables:
    C:\msys64\ucrt64\bin

🍏 For macOS Users:

xcode-select --install

🐧 For Linux (Ubuntu/Debian):

sudo apt-get update
sudo apt-get install build-essential

🏗️ Step 2: Setup VS Code

  1. Download and install VS Codehttps://code.visualstudio.com/

  2. Install the C/C++ Extension by Microsoft from the Extensions Marketplace.


📁 Step 3: Write Your Code

Here’s a sample C program:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Or a sample C++ program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, C++ World!" << endl;
    return 0;
}

Save your file as hello.c or hello.cpp in your project folder.


💻 Step 4: Compile and Run in Terminal

▶️ For C Code:

gcc hello.c -o hello
./hello

▶️ For C++ Code:

g++ hello.cpp -o hello
./hello

What’s happening here?

  • gcc or g++ → Compiler command

  • -o hello → Sets the output file name to hello

  • ./hello → Runs the compiled program


🪛 Common Errors & Fixes

ErrorFix
'gcc' is not recognized as...Ensure GCC/G++ is installed and added to PATH
permission denied (Linux/macOS)Run chmod +x hello before executing the file
Compilation errorsDouble-check your syntax and missing semicolons

⚡ Bonus: Automate Compilation (Optional)

Want to automate the compilation and execution process?

  1. Create a .vscode/tasks.json file in your project directory.

  2. Paste this configuration:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build and run C++",
      "type": "shell",
      "command": "g++",
      "args": [
        "hello.cpp",
        "-o",
        "hello",
        "&&",
        "./hello"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Now, press Ctrl + Shift + B in VS Code, and it will compile and run automatically.


✅ Conclusion

Congratulations! 🎉 You now know exactly how to manually compile and run C or C++ code in VS Code’s terminal.

This method may feel “old school,” but trust me—it’s how real developers learn the ropes. Once you master this, advanced topics like multi-file compilation and debugging will be much easier.

Have questions? Drop them in the comments or connect with me—I’m always here to help.


⚡ Follow for More Programming Tutorials

🌐 Website → https://codewithmishu.vercel.app
📸 Instagram → https://instagram.com/codewithmishu
🐦 Twitter → https://twitter.com/codewithmishu

#programming #c #cpp #vscode #developers #beginners

1
Subscribe to my newsletter

Read articles from Munish Kumar Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Munish Kumar Sharma
Munish Kumar Sharma

👋 Yo! I’m Munish — a full stack web dev + AI/ML learner building in public. 💻 On this channel: Beginner-friendly web development tutorials Dev vlogs + learning journey AI/ML experiments coming soon 🎯 Subscribe to grow with me and build cool stuff, one line of code at a time. #CodeWithMishu | Learn. Build. Dominate.