To compile C++ on Windows, install a free compiler, then build your .cpp file into an .exe. The usual route is Microsoft’s Visual Studio Community with the Desktop development with C++ workload: open the Developer Command Prompt, type cl /EHsc hello.cpp and run hello. Prefer GCC? Install MinGW-w64 through MSYS2 and type g++ hello.cpp -o hello.exe. These steps work on Windows 11 and Windows 10.
Pick a compiler route
A compiler turns the C++ source code you write (a text file ending in .cpp) into an executable (a .exe file Windows can run). Windows doesn’t include a C++ compiler, so you install one. Three free routes cover almost everyone:
| Route | Compiler | Best for |
|---|---|---|
| Visual Studio IDE | Microsoft C++ (MSVC) | Beginners who want one app that edits, builds, runs and debugs |
| Command line with MSVC | MSVC (cl.exe) | Learning what a build actually does, or using the smaller Build Tools install |
| Visual Studio Code with MinGW-w64 | GCC (g++) | Courses that use g++, or anyone who wants a lightweight editor |
If your class or book names a compiler, use that one; the code is the same C++, but command-line options differ between MSVC and GCC. If nobody has told you, start with Visual Studio. Every method below uses the same test program, saved as hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello from C++ on Windows!" << std::endl;
return 0;
}
Install Visual Studio with C++ support
Methods 1 and 2 both need Microsoft’s compiler. Visual Studio Community is free for individual developers, classroom learning, academic research and open source work, according to Microsoft’s installation guide; companies outside those uses need Professional or Enterprise.
- Prepare the PC. Microsoft suggests installing pending Windows updates, restarting, and freeing disk space before you begin, because Visual Studio is a large install.
- Download the installer. Get Visual Studio Community from Microsoft’s Visual Studio downloads page and run the file named VisualStudioSetup.exe from your Downloads folder. Select Yes at the User Account Control prompt and accept the license terms.
- Choose the workload. In the Visual Studio Installer, check Desktop development with C++. This is the part that adds the compiler, the Windows libraries and the debugger. Leave the optional components at their defaults.
- Install. Select Install and wait; it can take a while on a slow connection. Pick the install drive now if you need to, because Microsoft says you can only choose a different drive on the first install.
Only want the command line? Microsoft’s Build Tools for Visual Studio installs just the compilers, tools and libraries, without the editor. Choose the same Desktop development with C++ workload in its installer, then use Method 2.
Already have Visual Studio without C++? Open Visual Studio Installer from the Start menu, select Modify, check Desktop development with C++ and install it.
Method 1: Build and run in the Visual Studio IDE
- Create a project. Open Visual Studio and select Create a new project. Set the filters to C++, Windows and Console, choose Console App (the C++ one, with “++” on its icon), and select Next.
- Name it. Type a project name such as HelloApp, keep or change the location, and select Create. The new project opens with a .cpp source file ready to edit.
- Add your code. Replace what’s in the source file with the hello.cpp program above, and press Ctrl+S to save.
- Build. Choose Build > Build Solution. The Output window at the bottom reports the result; look for a line saying the build succeeded with 0 failed.
- Run. Choose Debug > Start Without Debugging, or press Ctrl+F5. A console window shows your message, followed by “Press any key to close this window . . .”.
Use Ctrl+F5 rather than F5 for quick tests. Microsoft explains that F5 starts a debugging session, and if the program needs no input and hits no breakpoint, the console window closes as soon as the program ends, so it looks like nothing happened.
The .exe is saved in a Debug folder (such as x64\Debug) inside your solution folder, so you can also run it later from a terminal without opening Visual Studio.
Method 2: Compile on the command line with cl
Microsoft’s compiler won’t run from a plain Command Prompt, because it needs environment settings that point to its tools, headers and libraries. Visual Studio installs shortcuts that set all of that up for you.
- Open the Developer Command Prompt. Select Start, then All apps, open the Visual Studio folder (not the Visual Studio app) and choose Developer Command Prompt for VS. You can also search Start for “developer command prompt”. Developer PowerShell for VS works too.
- Check the compiler. Type
cland press Enter. You should see a “Microsoft (R) C/C++ Optimizing Compiler” banner and a usage line. If you get an error instead, see the troubleshooting section before you go on. - Make a folder for your code. Type
md C:\hello, press Enter, thencd C:\hello. - Create the file. Type
notepad hello.cpp, select Yes when Notepad offers to create the file, paste the program, and save. Opening Notepad this way saves the file in the current folder with the .cpp extension. - Compile. Type
cl /EHsc hello.cpp. The compiler creates hello.obj, then the linker creates hello.exe. /EHsc turns on standard C++ exception handling; without it, Microsoft warns, thrown exceptions can leave objects undestroyed and leak resources. - Run. Type
helloand press Enter. The message appears on the next line.
Microsoft’s own command-line walkthrough uses the same steps and shows sample output for each one.
Useful cl options
- Several source files: list them all, as in
cl /EHsc main.cpp math.cpp. The program is named after the first file (main.exe). - Choose the output name: add the linker option at the end, as in
cl /EHsc main.cpp math.cpp /link /out:calc.exe. - More warnings: Microsoft suggests /W3 or /W4 to catch mistakes, for example
cl /W4 /EHsc hello.cpp. - A specific C++ standard: add /std:c++17 or /std:c++20 when a course or library requires one.
- Treat a file as C++ whatever its extension: /TP. By default MSVC compiles .c files as C and .cpp files as C++.
- See every option:
cl /?.
Worked example: a program in two files
Real programs are split into several files. Say Alex writes a small calculator with three files in C:\calc:
- math.h declares the function:
int add(int a, int b); - math.cpp defines it:
#include "math.h"followed byint add(int a, int b) { return a + b; } - main.cpp uses it:
#include <iostream>,#include "math.h", thenint main() { std::cout << add(2, 3) << std::endl; }
Only the .cpp files go on the command line; the compiler reads math.h because the #include lines name it. With MSVC, Alex types cl /EHsc main.cpp math.cpp /link /out:calc.exe, then calc, and sees 5. With g++ (Method 3), the equivalent is g++ main.cpp math.cpp -o calc.exe. If Alex forgets math.cpp, compiling still succeeds but linking fails with an “unresolved external symbol” (MSVC) or “undefined reference” (g++) error, which is the linker saying it can’t find the function’s code.
Method 3: Use g++ with MinGW-w64 and Visual Studio Code
MinGW-w64 is a Windows build of GCC, the compiler most Linux and many university courses use. Microsoft’s Visual Studio Code documentation recommends getting it through MSYS2, which supplies current builds. Older guides point to the original “MinGW” project or bundled installers; those builds are outdated, so use MSYS2. MSYS2 needs 64-bit Windows 10 version 1809 or later.
Install the compiler
- Install MSYS2. Download the installer from the MSYS2 website (msys2.org), run it, and keep the suggested install folder (C:\msys64). Leave Run MSYS2 now checked and select Finish; a terminal window opens.
- Install the toolchain. In that terminal, type
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain. Press Enter to accept the default packages, then type Y to proceed. - Add the compiler to PATH. PATH is the list of folders Windows searches when you type a command. Search Start for Edit environment variables for your account, select Path under User variables, select Edit, then New, and add
C:\msys64\ucrt64\bin. Select OK twice. - Check it. Open a new Command Prompt (windows opened before the change won’t see it) and type
g++ --version. You should see a version line.gdb --versionchecks the debugger too.
Compile with g++ from a terminal
- Save hello.cpp in a folder such as C:\projects\hello.
- Open Command Prompt or PowerShell and go there with
cd C:\projects\hello. - Type
g++ hello.cpp -o hello.exe. The -o option names the output file; without it, g++ picks a default name. - Run it: type
helloin Command Prompt, or.\hello.exein PowerShell.
For several files, list them all: g++ main.cpp math.cpp -o calc.exe. Add -g to include debugging information (VS Code’s generated build task uses it). Current GCC documentation lists GNU C++20 as the default standard for C++ code; use -std=c++17 or -std=c++23 when you need a different one.
Build from Visual Studio Code
- Install Visual Studio Code, then press Ctrl+Shift+X, search for C++ and install Microsoft’s C/C++ extension.
- Open your project folder (File > Open Folder, or type
code .in a terminal inside the folder) and select Yes, I trust the authors for a folder you created. - Open hello.cpp, select the play button at the top right of the editor, and choose C/C++: g++.exe build and debug active file.
- The output appears in the integrated Terminal. VS Code saves your build settings in .vscode\tasks.json, so later runs skip the compiler question.
The full walkthrough, including debugging and IntelliSense setup, is on the Using GCC with MinGW page in the VS Code docs.
Run your program in Command Prompt or PowerShell
Old tutorials say to type ./yourprogram. That’s Linux and macOS syntax and doesn’t work in Command Prompt. On Windows:
- Command Prompt: type the name, with or without .exe (
helloorhello.exe), while you’re in the program’s folder. - PowerShell: type
.\hello.exe. Microsoft notes that PowerShell won’t run a program from the current folder unless you give a path, and .\ means “this folder”. - Double-clicking the .exe in File Explorer works, but a console program closes its window the moment it finishes, so you’ll only see a flash. Run it from a terminal instead.
New to terminals? Here’s how to open Command Prompt on Windows 11, and the same commands work if you open Windows PowerShell instead (just add .\ before the program name).
Fix common compile errors
- ‘cl’ is not recognized as an internal or external command: you’re in a regular Command Prompt. Open the Developer Command Prompt for VS. Microsoft says the same cause applies to errors C1034 and LNK1104; if they persist in the Developer prompt, reinstall the MSVC component through Visual Studio Installer.
- ‘g++’ is not recognized: the PATH entry is missing or wrong, or the window was open before you changed PATH. Check that C:\msys64\ucrt64\bin exists and contains g++.exe, fix the entry, and open a new terminal.
- Cannot open source file or “No such file”: you’re in the wrong folder or the file is really hello.cpp.txt. Type
dirto list the folder. In Notepad, use File > Save As, set Save as type to All Files and type hello.cpp, as Microsoft’s walkthrough advises. - Errors pointing at a line number: that’s your code, not the setup. Check the line named in the message for a missing semicolon, brace or quote, save, and compile again. Fix the first error first; later ones are often side effects.
- “The value of miDebuggerPath is invalid” in VS Code: the debugger (gdb) may be missing. Re-run the pacman toolchain command, which includes it.
- The program builds but won’t run or can’t write files: Microsoft notes that, depending on your security settings, you may need to right-click the Developer Command Prompt and choose Run as administrator. Try a folder you own, such as one under your user folder, first.
Download compilers only from Microsoft, the VS Code site and msys2.org. Don’t turn off antivirus or security warnings to make a build work; the official installers don’t need it.
Check what you’ve built
You’ve succeeded when three things are true: the compiler finishes without errors, a .exe file appears in the folder (type dir to see it), and running it prints your message. To start fresh, delete the .exe and .obj files and compile again; the .cpp file is all you need to keep. If you want to remove a compiler later, uninstall Visual Studio from Visual Studio Installer, or uninstall MSYS2 like any other app and remove its PATH entry.
Questions beginners ask about C++ on Windows
Can I install both compilers?
Yes. Visual Studio and MSYS2 install in their own folders, and cl only runs from the Developer Command Prompt, so the two stay separate. Keep each project on one compiler so it always builds the same way.
Will my .exe run on another PC?
A simple console program usually will on another 64-bit Windows PC, but programs can depend on runtime libraries that the other PC lacks. When you share a program, test it on a machine that doesn’t have your compiler installed.
Is Visual Studio the same as Visual Studio Code?
No. Visual Studio is a full IDE that includes Microsoft’s compiler once you add the C++ workload. Visual Studio Code is a lighter editor with no compiler of its own; it uses whichever one you install, such as MinGW-w64 or MSVC.
If you’re setting up other languages on the same PC, the PATH and terminal habits from Method 3 carry over; for example, you can install Python on Windows next and check it the same way with a version command.
Matthew Burleigh has been a freelance writer since the early 2000s. You can find his writing all over the Web, where his content has collectively been read millions of times.
Matthew received his Master’s degree in Computer Science, then spent over a decade as an IT consultant for small businesses before focusing on writing and website creation.
The topics he covers for MasterYourTech.com include iPhones, Microsoft Office, and Google Apps.
You can read his full bio here.