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:
- Opening the file — to read or edit the code.
- 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:
- VS Code — free, works on every platform, and the best all-round choice.
- Visual Studio (Windows) — a full IDE with a compiler built in.
- Xcode (Mac) — Apple’s IDE.
- Notepad / TextEdit — work in a pinch, but with no colour highlighting.
Opening it in any of these won’t run anything. For that, keep reading.
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
- Install a compiler first (VS Code doesn’t include one) — see the setup guide.
- Install Microsoft’s C/C++ extension.
- Open the folder containing your
.cppfile (File → Open Folder). - 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)
- Create a new project → Empty Project (C++).
- In Solution Explorer, right-click Source Files → Add → Existing Item and pick your
.cppfile. - 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
| Goal | Command or action |
|---|---|
| Open / edit | VS Code, Visual Studio, Xcode, any text editor |
| Compile | g++ main.cpp -o main |
| Run (Mac/Linux) | ./main |
| Run (Windows) | .\main.exe or main.exe |
| Several files | g++ a.cpp b.cpp -o app |
| Use C++20 | g++ -std=c++20 main.cpp -o main |
| No install | An 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
Related Articles
- How to Set Up C++ — installing a compiler on every platform.
- Online C++ Compilers — run C++ with nothing installed.
- C++ Hello World Explained — what each line does.
- Undefined Reference Errors — the most common linker error.
- C++ Command Line Arguments — passing input when you run a program.