Skip to content
C++ Better Explained
Go back
How to Open and Run a .cpp File (Windows, Mac and Linux)

How to Open and Run a .cpp File

Short answer: a .cpp file is source code, not a program — opening it shows you the code, it doesn’t run it. To run it, compile it first, then run what the compiler produces:

g++ main.cpp -o main
./main          (Mac / Linux)
main.exe        (Windows)

Why Double-Clicking Doesn’t Run It

C++ isn’t run directly. A compiler turns your human-readable .cpp file into an executable — a file your computer can actually run.

main.cpp  ──compile──►  main.exe (or main)  ──run──►  output

So there are two separate jobs:

  1. Opening the file — to read or edit the code.
  2. Running it — which means compiling it and then running the executable.

How to Open a .cpp File

It’s plain text, so anything that edits text will open it:

Opening it in any of these won’t run anything. For that, keep reading.

Just getting started? Once your first program runs, the C++ Better Explained Ebook takes you through the fundamentals in 87 plain-English pages — with pointers, memory and OOP explained properly. Just $19.

Method 1: Compile and Run from the Command Line

This works everywhere once a compiler is installed, and it shows you what every other method is doing behind the scenes.

Here’s a file to test with — save it as main.cpp:

#include <iostream>

int main() {
    std::cout << "It works!\n";
    return 0;
}

Open a terminal in the folder containing the file, then:

Windows (MinGW-w64), Command Prompt or PowerShell:

g++ main.cpp -o main
.\main.exe

Mac or Linux:

g++ main.cpp -o main
./main

You should see It works!. The -o main part names the output file; without it, you get a.exe on Windows or a.out elsewhere.

More than one .cpp file? List them all:

g++ main.cpp utils.cpp -o app

Method 2: VS Code

  1. Install a compiler first (VS Code doesn’t include one) — see the setup guide.
  2. Install Microsoft’s C/C++ extension.
  3. Open the folder containing your .cpp file (File → Open Folder).
  4. Open the file and use the run (play) button the extension adds in the top-right corner.

Under the hood, VS Code runs the same g++ command as Method 1.

Method 3: Visual Studio (Windows)

  1. Create a new project → Empty Project (C++).
  2. In Solution Explorer, right-click Source Files → Add → Existing Item and pick your .cpp file.
  3. Press Ctrl+F5 (Start Without Debugging).

Ctrl+F5 keeps the console window open after the program finishes, which saves a lot of confusion.

Method 4: No Install at All

Paste the code into an online compiler such as OnlineGDB and press Run. It’s the fastest way to try something — see the best online C++ compilers.

Common Errors and Fixes

'g++' is not recognized as an internal or external command (Windows) Windows can’t find the compiler. Either it isn’t installed, or its bin folder isn’t on your PATH. Install MinGW-w64, add its bin folder to PATH, then open a new terminal — old windows don’t pick up the change.

command not found: g++ (Mac) Install Apple’s command line tools:

xcode-select --install

fatal error: main.cpp: No such file or directory The terminal isn’t in the folder that contains your file. Use cd to move there first, or give the full path.

The window flashes open and closes instantly (Windows) The program ran and finished. Run it from a terminal instead of double-clicking the .exe, or use Ctrl+F5 in Visual Studio.

undefined reference to ... Usually a missing .cpp file in the compile command, or a declared-but-never-defined function. See undefined reference errors.

Quick Reference

GoalCommand or action
Open / editVS Code, Visual Studio, Xcode, any text editor
Compileg++ main.cpp -o main
Run (Mac/Linux)./main
Run (Windows).\main.exe or main.exe
Several filesg++ a.cpp b.cpp -o app
Use C++20g++ -std=c++20 main.cpp -o main
No installAn online compiler such as OnlineGDB

Take Your C++ Further

Getting code to run is step one. Understanding it — pointers, memory and OOP especially — is where most beginners stall. The C++ Better Explained Ebook explains them in plain English. Just $19.

👉 Get the C++ Better Explained Ebook — $19


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
using namespace std in C++: What It Means and Why It's Discouraged
Next Post
Best C++ Books and Resources for Beginners in 2026

Keep Learning